第一。你使用函数strtotime看错了
PHP documentation
int strtotime ( string $time [, int $now = time() ] )
您需要修改代码以将整数时间戳传递到此函数中。
第二。你使用格式d.m.Y H:我包括时间部分。如果您只想比较日期,则必须删除时间部分,例如`$ date = date(“d.m.Y”);“
第三。我不知道它是否以同样的方式为您工作,但是我的PHP不明白$ timestamp的日期格式,并将01.01.1970 02:00返回到$ match_date
$timestamp = "2014.09.02T13:34";
date('d.m.Y H:i', strtotime($timestamp)) === "01.01.1970 02:00";
您需要检查strtotime($ timestamp)是否返回正确的日期字符串。如果否,则需要指定在$ timestamp变量中使用的格式。您可以使用功能date_parse_from_format或DateTime::createFromFormat之一来完成此操作
这是一个工作示例:
$timestamp = "2014.09.02T13:34";
$today = new DateTime(); // This object represents current date/time
$today->setTime( 0, 0, 0 ); // reset time part, to prevent partial comparison
$match_date = DateTime::createFromFormat( "Y.m.d\\TH:i", $timestamp );
$match_date->setTime( 0, 0, 0 ); // reset time part, to prevent partial comparison
$diff = $today->diff( $match_date );
$diffDays = (integer)$diff->format( "%R%a" ); // Extract days count in interval
switch( $diffDays ) {
case 0:
echo "//Today";
break;
case -1:
echo "//Yesterday";
break;
case +1:
echo "//Tomorrow";
break;
default:
echo "//Sometime";
}