簡體   English   中英

strotime比較的怪異行為

[英]Weird behaviour of strotime comparison

我無法理解我編寫的這段代碼有什么問題,以及為什么它具有不直觀的行為

if(($datav == 0) || ((strtotime($datav)) > (strtotime('01/01/2014')))) {
echo 'yes';
}

else if((strtotime($datav)) < (strtotime('01/01/2014'))) {
echo 'no';
        }

$ datav是一個日期變量,可以或不能以我編寫的wordpress形式設置。

這是發生了什么:如果未設置日期(== 0),則代碼有效,它回顯“是”; 如果設置了日期並且在2014年1月1日之前也可以使用,則會回顯“否”; 但是如果設置了日期並且在2014年1月1日之后,它將不起作用並回顯“否”。 在第三種情況下,我確定設置了正確的日期(2014年1月1日之后的日期),因為我回顯了它以進行檢查。

我究竟做錯了什么? 謝謝任何人。

<?php

$datav = 0;

test('2014-01-01');

test('2015-01-01');

test('01/01/2013');

test('25/12/2014'); // fail because strtotime will resolve as 1970-01-01

function test($datav) {

    echo "Input Date: $datav = ";

    $timestamp = strtotime($datav);

    if ($datav == 0 || $timestamp > strtotime('01/01/2014')) {
        echo 'yes';
    } else if ($timestamp < strtotime('01/01/2014')) {
        echo 'no';
    } else {
        echo 'neither';
    }

    echo " - What strtotime thinks was input (".date('d-M-Y', $timestamp).")<br />";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM