繁体   English   中英

检查datetime值是否来自最近30天的php(不在mysql查询中)?

[英]Check if datetime value is from last 30 days in php (not in the mysql query)?

我需要用php检查是否从数据库中获取的值是最近30天中的值。

值的格式如下:

2012-03-19 05:00:32

如何才能做到这一点?

您可以使用strtotime将其转换为unix时间戳。

$db_date = "2012-03-19 05:00:32";
if (time() - strtotime($db_date) <= 30 * 86400) {
  //...
}
$date = '2012-03-19 05:00:32';
if (strtotime($date) >= strtotime('-30 days')) {
    // do something
}

请参阅strtotime()参考

您可以在PHP中完成此操作,但这意味着通过日期/时间系统进行往返,以将该字符串处理回日期值:

$within_30 = ((strtotime('2012-03-19 05:00:32') + 30*86400) > time());

假设您使用的是MySQL,则可以直接在查询中进行操作,并节省一些时间转换:

SELECT ((yourtimefield + INTERVAL 30 DAY) > now()) AS within_30 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM