2017-06-16 60 views

回答

1

簡單使用的strtotime()函數

http://php.net/manual/en/function.strtotime.php

if(strtotime('Friday 19 may 2017') < strtotime('now')) { 
    echo 'do something'; 
} 

或(編輯,因爲BenM評論)

if(strtotime('Friday 19 may 2017') < time()) { 
    echo 'do something'; 
} 
+1

'strtotime('now')'==='time()'。 – BenM

+0

謝謝Raymond! – Bas

1

使用DateTime類PHP:

$date = new DateTime('2017-05-19 00:00:00'); 
$now = new DateTime(); 

if($date < $now) 
{ 
    // Do your stuff. 
} 

不過,既然你正在處理一個特定的日期,它肯定會永遠成爲過去?

+0

好點(關於具體日期) – ThisGuyHasTwoThumbs

0

可以使用date來實現這一目標:

<?php 
    $date = date('Y-m-d', strtotime('Friday 19th May 2017')); 
    $today = date('Y-m-d'); 

    if ($date < $today) { 
     //do some code 
    } 

這是什麼一樣,是通過使用strtotime它創建基於STR值的日期/時間分配$date今天等於。

$today被分配爲date()這將返回服務器的當前時間戳,而不是用戶計算機的本地時間。然後比較日期。

+0

爲什麼在'strtotime('2017年5月19日星期五') BenM

+0

@BenM我想這是更優雅的方式:) – ThisGuyHasTwoThumbs