将一个小时设为时间戳
如何将时间(例如 09)设置为 UNIX 时间戳?我尝试了 strtotime()
但没有成功。 strtotime("9")
不返回任何内容。
编辑:
这是我的错,我之前没有提到,但我试图检查当前时间+ 30 分钟是否大于或等于保存在 mysql 数据库中的时间。例如,我在数据库中将时间保存为 09(可能是我的错误)。
How can I make a time, for example 09, to a UNIX timestamp? I tried strtotime()
but unsuccessfully. strtotime("9")
returns nothing.
Edit:
It's my fault that I didn't mention before but I'm trying to check if current time + 30 minutes is bigger or equal to time, saved in a mysql DB. I save time in DB as 09, for example (probably there's my mistake).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 mktime
http://php.net/manual/en/function.mktime.php
Try mktime
http://php.net/manual/en/function.mktime.php
虽然到目前为止两个答案可能都更好,但我想我会提供一个仍然使用 strtotime() 的“可爱”解决方案:
演示:
While both answers so far are probably better, I thought I'd offer a "cute" solution that still uses
strtotime()
:Demo: http://codepad.org/k9U4BiKN
您在编辑中提供的附加信息澄清了一两件事。如果数据库值是表示小时的字符串,则生成用于比较的时间戳可能不会有太大作用。在这种情况下,您也可以将小时值与数字进行比较:
如果数据库小时值在 0 到 23 之间,则比较当然会在 22:30 到 23:29 之间始终返回 true。
注意:如果 MySQL 表字段是时间戳字段,则可以使用“SELECT EXTRACT(HOUR FROM your_field) AS db_hour FROM your_table;”仅获取小时部分。
http://dev.mysql.com /doc/refman/5.5/en/date-and-time-functions.html#function_extract
The additional information you provided in the edit clarifies one or two things. Generating a timestamp for comparison probably won't do much good if the database value is a string representing the hour. In this case, you can just as well compare the hour values as numbers:
If the database hour values are between 0 and 23, the comparison will of course always return true between 22:30 and 23:29.
Note: Had the MySQL table field been a timestamp field, you could get just the hour part with "SELECT EXTRACT(HOUR FROM your_field) AS db_hour FROM your_table;"
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_extract