您可以使用strtotime()
更大:
$OldDate = "2011-09-30";
$oldDateUnix = strtotime($OldDate);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
UPDATE
因爲您使用的是非常規日期戳,您必須使用不同的方法,例如:
$OldDate = "09-30-2011";
list($month, $day, $year) = explode("-", $OldDate);
$oldDateUnix = strtotime($year . "-" . $month . "-" . $day);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
注:如果你想總是確保您的日期被正確地strtotime
瞭解,使用YYYY-MM-DD
很快。 – wesside
然而,使用strtotime時要小心,如果你期望美國格式化的日期,並得到英國例如,你可以得到一些令人困惑的結果。 – scoota269
@ scoota269是正確的 - 你可以用'echo date(「Y-m-d」,$ oldDateUnix)''來測試它。在這種情況下,它甚至會出現bug和結果(1970-01-01),這是unix時間0.我更新了我的最新評論以修復和反映此問題。 – h2ooooooo