PHP:到某个日期剩下的天、小时和分钟


PHP: Days, hours and minutes left to a certain date?

我正在尝试使用 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的事实?

有人可以就此问题提供建议吗?

只需像 as 一样使用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");

试试这个

<?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");
?>

试试这个。

$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));