我如何检查格式为“2008-02-16 12:59:57”的日期是否少于24小时?
答案 0 :(得分:26)
if (strtotime("2008-02-16 12:59:57") >= time() - 24 * 60 * 60)
{ /*LESS*/ }
答案 1 :(得分:20)
使用strtotime
的相对日期添加另一个答案:
$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
// Do something
}
我认为这使代码更具可读性。
答案 2 :(得分:4)
if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
// less than 24 hours ago
}
答案 3 :(得分:2)
e.g。通过strtotime和time()。
差异必须小于86400(每天秒数)。
<?php
echo 'now: ', date('Y-m-d H:i:s'), "\n";
foreach( array('2008-02-16 12:59:57', '2009-12-02 13:00:00', '2009-12-02 20:00:00') as $input ) {
$diff = time()-strtotime($input);
echo $input, ' ', $diff, " ", $diff < 86400 ? '+':'-', "\n";
}
打印
now: 2009-12-03 18:02:29
2008-02-16 12:59:57 56696552 -
2009-12-02 13:00:00 104549 -
2009-12-02 20:00:00 79349 +
过去只有最后一次测试日期/时间不到24小时。
答案 4 :(得分:2)
Php有一个comparison function between two date/time objects,但我并不是非常喜欢它。这可能是不精确的。
我所做的是使用strtotime()从日期对象中生成一个unix时间戳,然后将其与time()的输出进行比较。
答案 5 :(得分:0)
只需使用它.....
if(strtotime($date_start) >= strtotime($currentDate))
{
// Your code
}
答案 6 :(得分:0)
应该有变量日期赞
$date_value = "2013-09-12";
$Current_date = date("Y-m-d"); OR $current_date_time_stamp = time();
您可以将转换日期后的日期与时间戳进行比较,以便:
if(strtotime($current_date) >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
OR
if($current_date_time_stamp >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
答案 7 :(得分:0)
也许它会更容易理解......
$my_date = '2008-02-16 12:59:57';
$one_day_after = date('Y-m-d H:i:s', strtotime('2008-02-16 12:59:57 +1 days'));
if($my_date < $one_day_after) {
echo $my_date . " is less than 24 hours ago!";
} else {
echo $my_date . " is more than 24 hours ago!";
}
答案 8 :(得分:0)
您可以使用Simple PHP执行此操作:
$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();
请检查教程。 Click here