2015-10-15 62 views
0

我需要比较PHP中的日期,以检查合同是否已过期。在PHP中使用时间戳

现在我这样做:

foreach ($this->array as $row) 
{ 
    if(strtotime($date1) > strtotime($row['end_date'])) 
    { 
    $delay = strtotime($row['end_date']); 
    $expired = "V"; 
    } 
    else { 
    $delay = strtotime($row['end_date']); 
    $expired = "X"; 
    } 

所以我只知道,如果合同已经到期。我需要写:

echo "The contract will expire in" . $delay; 

和输出是:

The contract will expire in 3 days. 

感谢您的帮助。

+0

已经回答:[如何比较PHP 5.2.8中的两个DateTime对象?](http://stackoverflow.com/questions/961074/how-do-i-compare-two-datetime-objects-in- php-5-2-8) – ThinkTank

+0

我不明白这是如何解决我的问题的。 –

+1

您需要将strtotime($ row ['end_date'])'与'time()'进行比较,你在他们之间的秒数,然后将其转换为日期 – Adam

回答

1
$interval = strtotime($date1) - strtotime($row['end_date']); // ... or replace $date1 

$delayInDays = floor($interval/(60 * 60 * 24)); 

echo "The contract will expire in: " . $delayInDays; 

如果你有兴趣在面向对象的方式来实现这一点,你可以看看DateTime::diff方法。