你的例子看起来不错。实际上,我更关心您打算如何处理闰年/夏令时。
如果使用 PHP 5.3 或更高版本。除了坚持使用时间戳,我建议(支持夏令时等):
程序
$datetime1 = date_create('now');
$datetime2 = date_create('2013-03-07');
$interval = date_diff($datetime1, $datetime2);
$days = $interval->format('%R%a');
if ((int) $days >= 2) {
echo '2 or more days';
echo '<br />';
echo 'Difference: ' . $days . ' days';
} else {
echo 'Bearly ' . $days . ' day or less';
}
目的
$date1 = new DateTime("2013-03-09");
$date2 = new DateTime("now");
$interval = $date1->diff($date2);
$difference = $interval->days;
if($difference >= 2){
echo $difference;
}
您可以尝试直接使用mysql:
阅读:https ://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
mysql> SELECT INTERVAL 1 DAY + '2008-12-31';
-> '2009-01-01'
Mysql date_add() and datediff() are also good alternatives.
PHP 日期时间差异:http ://www.php.net/manual/en/datetime.diff.php