2014-09-23 249 views
0

我需要獲得日期到達之前的日子,如果時間戳在1990年之前的當前日期之前,那麼它將顯示一條消息。如何確定unix時間戳日期之前剩餘多少天?

所以說它是2014年11月1日 如果時間戳在2014年11月1日之前,那麼它將顯示過期,否則它會告訴你在達到日期之前還有多少天。

非常感謝你。 這是在順便說一句。

+0

到目前爲止你想出了什麼?時間過得如何?沒有看到這個,我們不能給你一個相關的答案。 – mschuett 2014-09-23 02:16:57

回答

0
<?php 

$current = time(); 
$target = '2014-11-01 00:00:00'; 
$target = strtotime($target); 

if($target > $current){ 

    $span = $target - $current; 
    $span = ceil($span/(60 * 60 * 24)); 

    echo 'You have less than '.$span.' days!'; 

} else { 

    echo 'Time has already expired!'; 

} 

上面會輸出

You have less than 39 days! 
+0

地板應該用來代替ceil – 2017-08-12 09:25:34

0

我會做的是設置一個DateTime Class你有每個unix時間戳。將兩個對象與DateTime的Diff Function進行比較。

$TodaysDate = new DateTime(); 
$TodaysDate->setTimestamp(time()); 
$ExperationDate= new DateTime('2014-10-01'); 
$interval = $TodaysDate->diff($ExperationDate); 

If($interval <= 0){ 
    echo 'Product Expired'; 
} 

他們有多種方式可以使用時間戳或關鍵字或特定日期格式在DateTime對象中設置時間。

0

只是減去你的時間戳和除以當前時間 - Unix時間以秒爲單位。