2017-08-07 40 views
-2

我想比較兩個日期,但不幸的是,這不是通過將其轉換爲UNIX格式與strtotime工作。我試圖比較日期到另一個日期。爲什麼strtotime不工作php

但是這種格式的工作:

if(strtotime("22-04-17") < strtotime("25-05-17")){ 
    echo 'Date One is smaller than date two'; 
} 

但很多時候,它的失敗。我在網上看到了很多例子,但我找不出什麼好的東西!

if(strtotime("22-04-17") < strtotime("04-05-17")){ //passing still the 
    // bigger on but not working 
    echo 'Date One is smaller than date two'; 
} 
+0

[比較兩個日期(的可能的複製https://stackoverflow.com/questions/3847736/comparing-two-dates ) – GrumpyCrouton

+3

'strtotime'的默認格式是'Ymd' –

+0

@GrumpyCrouton這實際上非常複雜。我只是試圖比較兩個日期。所以請給我一個更簡單的例子。 –

回答

0

我知道這不是你要找的,但你有沒有試過這樣做來顯示日期是否更大/更小?

// Dates 
$Date1 = strtotime("22-04-17 GMT"); 
$Date2 = strtotime("04-05-17 GMT"); 

// Diff between dates 
$Diff = (int)floor(($Date1 - $Date2)/(60*60*24)); 

if ($Diff < 0) { 
    echo "The Diff is negative"; 
} 

那麼其他的方法是這樣的答案:LINK

$date1 = strtotime("22-04-17 GMT"); 
$date2 = strtotime("04-05-17 GMT"); 

if((int)strtotime($date1) < (int)strtotime($date2)){ //passing still the 
    echo 'Date One is smaller than date two'; 
} 
+0

這一個也不像高雅我。 –

+0

@CodeCooker這是我用來計算日期之間的不同,也可以是更大或更小。 你也可以使用'DateTime'對象來檢查你需要誠實的 –

+0

我正在嘗試使用日期選擇器。這可以幫助我以這種格式'22-04-17'在兩個日期之間從服務器獲取數據。請問我該怎麼做? –

1

你comparsion不能正常工作,因爲strtotime("22-04-17")實際結果與時間戳這個日期:17th April 2022;

請執行以下操作,您將看到我的意思。下面的代碼將輸出「2022五月-17`

$date = "22-05-17"; 
echo date ("Y-M-d ",strtotime($date))."<br>"; 
+0

我不是在這裏談判。我想要一個解決方案,請幫助我,如果你能 –

+1

我不想與你談判。我告訴你的是爲什麼你的代碼不工作。由於您選擇了'-'作爲分隔符,並且您的年份是2位數,因此PHP將格式設置爲'y-m-d'。這裏的解決方案是使用4位數年份。 –

3

manual(作特別說明,我已經把在大膽的部分):

「日期在M/d/y或dmy格式通過查看各個組件之間的分隔符來消除歧義:如果分隔符是斜槓(/),則假設爲美國m/d/y;而如果分隔符是破折號( - ),或點(。),則假定歐洲dmy格式但是,如果年份以兩位數格式給出,並且分隔符是短劃線( - ,則日期字符串是解析爲y-m-d。

因此,這裏是你在做什麼,與日期PHP的解釋你的字符串作爲註釋:

// Is 17 April 2022 earlier than 17 May 2025? Yes. 
if(strtotime("22-04-17") < strtotime("25-05-17")){ 
    echo 'Date One is smaller than date two'; 
} 

// Is 17 April 2022 earlier than 17 May 2004? No. 
if(strtotime("22-04-17") < strtotime("04-05-17")){ //passing still the 
    // bigger on but not working 
    echo 'Date One is smaller than date two'; 
} 

我希望這是你有明確的問題

因爲它還在手冊中說,使用DateTime::createFromFormat/date_create_from_format如果你想避免歧義:

$date = date_create_from_format('d-m-y', '04-05-17'); // 4 May 2017 
+0

是的,這是優雅的理解問題。那我該如何解決這個問題? –

2

試試這個

$date1 = date('d-m-y',strtotime("22-04-17")); 
$date2 = date('d-m-y',strtotime("04-05-17"));; 

if((int)strtotime($date1) < (int)strtotime($date2)){ //passing still the 
    echo 'Date One is smaller than date two'; 
} 

你一年格式17導致問題strtotime功能

+0

這一個工作!我不知道是否有錯誤。你在生產中使用過這個嗎? –

+0

@CodeCooker而不是'date('dm-y',strtotime(「22-04-17」));'和'date('dm-y',strtotime(「04-05-17」));'你可以把'strtotime(「22-04-17 GMT」);'和'strtotime(「04-05-17 GMT」);' –

+0

這是有點誤導,因爲如果你做'$ date1 = date('dm -Y」,的strtotime( 「22-04-17」)); $ date2 = date('d-m-Y',strtotime(「04-05-17」)); ((int)strtotime($ date1)<(int)strtotime($ date2)){//仍然通過 echo'Date One小於日期2'; }'這個失敗了。 –

0

我寫了一個函數,你datestring和正確返回TIMESTMP。請注意,我也跟着治療2位數年的PHP的會議,i.e. 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999

/* functon takes dateString of format dd-dd-yy and return proper timestamp */ 
function getTimestamp($dateString) 
{ 
    $res = preg_match('/^([0-9]{2})\-([0-9]{2})-([0-9]{2})/', $dateString, $matches); 
    if(!$res) 
     return 0; 
    //00-69 are mapped to 2000-2069 and 70-99 to 1970-1999 
    if($matches[3]>=70 && $matches[3]<=99) 
     $year = "19".$matches[3]; 
    else 
     $year = "20".$matches[3]; 
    $formatted_dat_string = $year."-".$matches[2]."-".$matches[1]; 
    return strtotime($formatted_dat_string); 
} 
getTimestamp("22-04-99"); 

所以你現在可以使用此功能,而不是strtotime進行比較。

0

最後,我得到了解決方案。 strtotime()不適合處理這種情況。您應該改爲使用dateTime對象。

//First you need to pass the original format then you will need to pass new 
//Format to get this working properly. Hope this will help you guy's 
$myDateTimestart = DateTime::createFromFormat('d-m-Y', $dateString); 
$startDate = $myDateTimestart->format('m-d-Y'); 
//with Simply that you can format your date properly 

我搞砸我的時間這個東西這是非常糟糕的