第一。你有錯誤,使用功能strtotime
看到PHP documentation
int strtotime (string $time [, int $now = time() ])
您需要修改代碼以整數時間戳進入此功能。
二。您使用的格式爲d.m.Y H:i包含時間部分。如果您只想比較日期,則必須刪除時間部分,例如`$ 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";
}
'$ timestamp'是如何定義的以及在哪裏? – 2014-09-02 11:31:59
使用strtotime檢查日期 – Khushboo 2014-09-02 11:32:37
我建議你看看**日期時間**對象..而且很可能你的$ timestamp沒有被正確轉換。 – Naruto 2014-09-02 11:34:33