gpt4 book ai didi

php - Date for Ranging 中的 SQL WHERE 子句计算

转载 作者:搜寻专家 更新时间:2023-10-30 21:53:22 26 4
gpt4 key购买 nike

我有一个项目,其中一个模块需要使用一组特定条件来显示数据列表:

Today
Week
Month
Year

数据库表包含一个数据类型为 BIGINT(10) 的日期字段,我们通过 PHP 代码将 time() 函数值插入其中。插入一些值,如 1311144077,

现在我需要根据上面提到的标签从我的表中获取记录,我该怎么做?

最佳答案

当您以纪元时间戳格式存储日期时间时,我认为使用 MySQL 日期/时间函数的解决方案可能会非常慢。因此,我建议像下面的代码一样使用时间戳本身。

使用示例

$t = time();

# today
echo date('Y-m-d H:i:s', strtotime('today', $t))."\n";
echo date('Y-m-d H:i:s', strtotime('tomorrow', $t))."\n";

# this week
echo date('Y-m-d H:i:s', strtotime('-1 sunday', $t))."\n";
echo date('Y-m-d H:i:s', strtotime('sunday', $t))."\n";

# this month
echo date('Y-m-d H:i:s', strtotime(date('Y-m-01 00:00:00', $t)))."\n";
echo date('Y-m-d H:i:s', strtotime(date('Y-m-01 00:00:00', strtotime('next month', $t))))."\n";

# this year
echo date('Y-m-d H:i:s', strtotime(date('Y-01-01 00:00:00', $t)))."\n";
echo date('Y-m-d H:i:s', strtotime(date('Y-01-01 00:00:00', strtotime('next year', $t))))."\n";

查询中

# this year
$start_of_year = strtotime(date('Y-01-01 00:00:00', $t));
$end_of_year = strtotime(date('Y-01-01 00:00:00', strtotime('next year', $t)));
$query = "SELECT * FROM sometable WHERE somecolumn >= $start_of_year AND somecolumn < $end_of_year";

关于php - Date for Ranging 中的 SQL WHERE 子句计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com