2016-01-27 102 views
1

我試圖讓剩下的天,小時和分鐘到使用php的某個日期。PHP:剩下幾天,幾小時和幾分鐘到某個日期?

但是我從我的代碼很奇怪的輸出,看起來像這樣:

-16828 days and -11 hours and -21 minutes and -24 seconds 

未來一段時間內存儲在該格式的mysql數據庫:

29/01/2016 7pm 

所以我徑自並做到這一點:

$Draw_time = "29/01/2016 7pm"; 

$date = $Draw_time; 
$timestamp = strtotime($date); 
$new_date = date('Y-m-d a',$timestamp); 

$seconds = strtotime($new_date) - time(); 

$days = floor($seconds/86400); 
$seconds %= 86400; 

$hours = floor($seconds/3600); 
$seconds %= 3600; 

$minutes = floor($seconds/60); 
$seconds %= 60; 


echo "$days days and $hours hours and $minutes minutes and $seconds seconds"; 

但是,當我運行此代碼,我得到上述奇怪的輸出!

據我所知,這可能是由於一些原因,但我能想到的唯一的事實是,我在使用我的格式a

請問有人能就這個問題提出建議嗎?

+1

的[PHP倒計時日期](http://stackoverflow.com/questions/1735252/php-countdown-to-date) –

+5

的[的strtotime可能的複製()不使用dd工作可能的重複/ mm/YYYY格式](http://stackoverflow.com/questions/2891937/strtotime-doesnt-work-with-dd-mm-yyyy-format) – AleFranz

+0

你使用哪種類型的商店'29/01/2016年7月下旬? :) –

回答

2

只需使用DateTime類等作爲

$Draw_time = "29/01/2016 7pm"; 

$date = DateTime::createFromFormat("d/m/Y ha",$Draw_time); 
$date2 = new DateTime(); 

echo $diff = $date2->diff($date)->format("%a days and %H hours and %i minutes and %s seconds"); 
0

試試這個

<?php 

    $Draw_time = str_replace('/', '-', "29/01/2016 7pm"); 

    $now = new DateTime(); 

    $futureDate = new DateTime($Draw_time); 

    $interval = $futureDate->diff($now); 
    echo $interval->format("%a days %h hours %i minutes %s seconds"); 
?> 
0

試試這個。

$draw_time = "2016/01/29 7pm"; 
$date_time = explode(" ", $draw_time);// make separate date and time in array 
$date = strtotime($date_time[0]); // convert your date(2016/01/29) into php time 
$time = strtotime($date_time[1]); // convert your time(7pm) into php time 
$date = $date + $time; // make total time to count 
$new_Date = $date - (time()); // convert into difference from current time 
$day = $new_Date % 86400; 
$hrs = $new_Date % 3600; 
$min = $new_Date % 60; 
echo "Day= ".(date("d",$day)); 
echo " Hours= ".(date("h",$hrs)); 
echo " Minutes= ".(date("i",$min)); 
相關問題