我以這種形式存儲mysql數據庫中的帖子日期y/m/d我想檢查帖子是否大於3天。有什麼辦法可以將它轉換爲秒?然後檢查,像這樣:php日期轉換爲秒
if($mysql_date > $three_days_old){
echo "old post";
}else {
echo "new post";
}
我以這種形式存儲mysql數據庫中的帖子日期y/m/d我想檢查帖子是否大於3天。有什麼辦法可以將它轉換爲秒?然後檢查,像這樣:php日期轉換爲秒
if($mysql_date > $three_days_old){
echo "old post";
}else {
echo "new post";
}
使用的date()
混合和strtotime()
:
$myDate = date('Y-m-d', strtotime($mysql_date));
$oldDate = date('Y-m-d', strtotime('3 days ago'));
if ($myDate > $oldDate) {
echo "old post";
} else {
echo "new post";
}
嘗試,strtotime,可以解析各種time string formats。
你可以做到以下幾點:
$d1 = strtotime(date('Y-m-d', strtotime($mysql_date)));
$d3 = mktime(0, 0, 0, date('m', $d1), date('d', $d1)-3, date('Y', $d1));
$d2 = strtotime(date('Y-m-d', strtotime('3 days ago')));
if ($d3 > $d2) {
echo 'old post';
}else {
echo 'new post';
}