Date for Ranging 中的 SQL WHERE 子句计算

发布于 2024-12-01 14:50:37 字数 212 浏览 2 评论 0原文

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

Today
Week
Month
Year

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

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

I Have project in which a module needs to display the list of data using a group of specific criteria:

Today
Week
Month
Year

The Database table contains a date field which has datatype as BIGINT(10) and through PHP code we insert time() function value into it. which insert some value like 1311144077,

Now I need to fetch the records from my table as per the labels mentionedabove, how can i do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久隐师 2024-12-08 14:50:37

当您以纪元时间戳格式存储日期时间时,我认为使用 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";

While you're storing datetime in epoch timestamp format, I think solutions using MySQL date/time functions could be very slow. So, I suggest using timestamp itself like below codes.

Usage example

$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";

In query

# 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";
嘦怹 2024-12-08 14:50:37

您可以使用 strtotime() 函数获取下限时间戳,例如。 strtotime('-1 week'),然后查询数据库中日期字段值大于该值的所有记录。

例如。要获取一周前或更新的行,您可以执行以下操作:

$ts = strtotime('-1 week');
// or $ts = strtotime('date'); where date can be a date in Y-m-d format
mysql_query("SELECT * FROM table WHERE your_bigint_column >= $ts");

如果需要按月分组,则可以使用 用于处理日期的 MySQL 函数

SELECT WEEK(FROM_UNIXTIME(your_bigint_column)) AS no_of_week, count(*)
FROM table
GROUP BY WEEK(FROM_UNIXTIME(your_bigint_column))

在一个不相关的注释中 - BIGINT(10) 是一种矫枉过正, time() 的结果可以安全地存储在 INT 列中。

You can get lower bound timestamp with strtotime() function, eg. strtotime('-1 week'), and then query your database for all records with date field's value greater than that.

Eg. to get a rows that are a week old or newer, you can do:

$ts = strtotime('-1 week');
// or $ts = strtotime('date'); where date can be a date in Y-m-d format
mysql_query("SELECT * FROM table WHERE your_bigint_column >= $ts");

If you need to group by month, you can just use MySQL functions for processing dates:

SELECT WEEK(FROM_UNIXTIME(your_bigint_column)) AS no_of_week, count(*)
FROM table
GROUP BY WEEK(FROM_UNIXTIME(your_bigint_column))

On an unrelated note - BIGINT(10) is an overkill, time()'s result can be safely stored in an INT column.

好久不见√ 2024-12-08 14:50:37

此命令会将数据库日期(“j F,Y”,“1222041600”)中保存的时间戳更改为1-6-1987,然后您将拥有月日和年份,并且您也可以计算周。有关更多详细信息,请参阅 php 日期函数。

this command will change the timestamp saved in database date("j F,Y", "1222041600") as 1-6-1987 then you will be having month day and year and on this you can calculate week too. See php functions of date for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文