unix 时间戳和 php

发布于 2024-08-31 03:41:11 字数 135 浏览 4 评论 0 原文

我的数据库中有一个 unix 时间戳列表,我想选择今天的时间戳。

即如果今天是星期二,我想获取今天的所有时间戳?是否可以?有没有诸如strtotime("Today")之类的东西?

任何帮助都会很棒

I have a list of unix timestamps in a database, and I wanting to select the ones that are from today.

i.e If today is Tueday, I want to get all the timestamps that were made today? Is it possible? Is there such a things as strtotime("Today")?

Any help would be great

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

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

发布评论

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

评论(6

一花一树开 2024-09-07 03:41:11

您可以使用 mktime() 生成开始的时间戳日期,然后查找时间戳大于该日期的数据库条目。

you can use mktime() to generate the timestamp for the start of the day and then find the database entries with a timestamp greater than that.

若能看破又如何 2024-09-07 03:41:11
$start = strtotime(date('Y-m-d 00:00:00')); // Current date, at midnight
$end = strtotime(date('Y-m-d 23:59:59')); // Current date, at 11:59:59 PM

然后,您可以选择上述两个时间戳之间的时间戳:

"SELECT FROM `foo` WHERE `timestamp` BETWEEN '{$start}' and '{$end}'"
$start = strtotime(date('Y-m-d 00:00:00')); // Current date, at midnight
$end = strtotime(date('Y-m-d 23:59:59')); // Current date, at 11:59:59 PM

then, you can just select where the timestamp is between the above 2 timestamps:

"SELECT FROM `foo` WHERE `timestamp` BETWEEN '{$start}' and '{$end}'"
吃→可爱长大的 2024-09-07 03:41:11

您可以使用 FROM_UNIXTIME() 将 unix 时间戳转换为 SQL 中的 SQL 日期,然后将它们与 NOW() 进行比较

SELECT * FROM `tablename` WHERE DATE(FROM_UNIXTIME(`dateFld`)) = DATE(NOW());

You can convert the unix timestamps to sql dates in the SQL using FROM_UNIXTIME(), then compare those to NOW()

SELECT * FROM `tablename` WHERE DATE(FROM_UNIXTIME(`dateFld`)) = DATE(NOW());
空城旧梦 2024-09-07 03:41:11

检查 DAY(NOW()) 和 MONTH(NOW()) 和 YEAR(NOW()) 是否等于 DAY(timestamp) 和 MONTH(timestamp) 和 YEAR(timestamp) 的适当值。

select timestamp from table where DAY(NOW()) = DAY(timestamp) AND MONTH(NOW()) = MONTH(timestamp) AND YEAR(NOW()) = YEAR(timestamp)

Check if DAY(NOW()) and MONTH(NOW()) and YEAR(NOW()) is equal to appropriate value of DAY(timestamp) and MONTH(timestamp) and YEAR(timestamp).

select timestamp from table where DAY(NOW()) = DAY(timestamp) AND MONTH(NOW()) = MONTH(timestamp) AND YEAR(NOW()) = YEAR(timestamp)
浸婚纱 2024-09-07 03:41:11

如果您使用的是 mysql:

SELECT * FROM table WHERE DATE(NOW()) = DATE(FROM_UNIXTIME(timestampcol))

If you're using mysql:

SELECT * FROM table WHERE DATE(NOW()) = DATE(FROM_UNIXTIME(timestampcol))
假情假意假温柔 2024-09-07 03:41:11

FROM_UNIXTIME(somefield ) 可以与 CURDATE() 假设您使用的是 MySQL

SELECT * FROM mytable WHERE FROM_UNIXTIME(datefield,'%Y-%m-%d') = CURDATE();

ETA:
好吧,当这个答案被标记下来时,我受到了怀疑。所以我去做了一些测试。考虑到 MySQL 它绝对有效。那么为什么要降级呢?

考虑这个测试,它为表中的每一行输出 2 个相同的字段:

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d')  a , CURDATE() b
  FROM tablewithsomerows 
  WHERE FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d') = CURDATE();

FROM_UNIXTIME(somefield) can be compared to CURDATE() assuming you're using MySQL

SELECT * FROM mytable WHERE FROM_UNIXTIME(datefield,'%Y-%m-%d') = CURDATE();

ETA:
Okay, I was assailed by doubt when this answer was marked down. So I went and did a couple of tests. Given MySQL it definitely works. So why the downmod?

Consider this test which outputs 2 identical fields for every row in a table:

SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d')  a , CURDATE() b
  FROM tablewithsomerows 
  WHERE FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d') = CURDATE();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文