[英]PHP Checking Date Past
我正在使用RFC822日期格式,並嘗試使我的if語句正常工作,但是它無法正常工作,我無法弄清楚為什么,這就是數據所反映的原因:
$currentdate = Fri, 01 Mar 13 22:24:02 +0000
$post['created_on'] = Sat, 17 Nov 2012 19:26:46 +0100
這是我的聲明:
$currentdate = date(DATE_RFC822, strtotime("-7 days"));
if ($post['created_on'] < $currentdate)
{
echo "test";
}
else
{
}
我正在嘗試檢查在其上創建的數組是否在最近7天內,我認為它與語句中的“ <”或日期格式化的方式有關?
謝謝西蒙
您要比較時間戳:
<?php
if (strtotime($post['created_on']) >= strtotime('-7 days'))
{
// Created in the last seven days
}
在進行字母數字比較時,代碼無法工作。 RFC822並非為此目的而設計。
請注意, Fri ...
低於Sat ...
是這樣的比較,因為F
在字母表中比S
早。
使用DateTime
類:
$currentdate = new DateTime('-7days +0100'); // ! use the same tz offset as the post !
$postdate = new DateTime('Sat, 17 Nov 2012 19:26:46 +0100');
if($postdate < $currentdate) {
// ... do stufff
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.