2016-01-27 101 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)); 
相关问题