簡體   English   中英

PHP:檢查將來是否使用歐盟日期格式

[英]PHP: check if EU date format is in future

我怎樣才能最好地檢查以下日期(格式為dd / mm / yyyy)是否是將來的日期:01/03/2011

為了澄清,這是2011年3月1日。

謝謝。

編輯:通過date_default_timezone_set('Europe / London');設置時區;

$date = "01/03/2011";

// convert the date to a time structure
$tmarr = strptime($date, "%d/%M/%Y");

// convert the time structure to a time stamp representing the start of the day
$then = mktime(0, 0, 0, $tmarr['tm_mon']+1, $tmarr['tm_mday'], $tmarr['tm_year']+1900);

// get the current time
$today = mktime(0, 0, 0);

// compare against the current date
if ($then > $today) {
    echo "$date is in the future";
}
elseif ($then == $today) {
    echo "$date is today";
}
else {
    echo "$date is not in the future";
}

您可以使用strtotime,並與當前日期進行比較。

首先,您需要將/更改為-才能將其解釋為歐洲日期。

通過查看各個組成部分之間的分隔符,可以消除m / d / y或dmy格式的日期的歧義:如果分隔符為斜杠(/),則假定為美國m / d / y; 相反,如果分隔符是破折號(-)或點(。),則采用歐洲dmy格式。

所以放在一起:

$time = strtotime(str_replace("/","-",$date) )
if ($time > time())
{
  echo "$date is in the future."
}

暫無
暫無

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

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