我正在尝试使用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?
有人能就这个问题提出建议吗?
发布于 2016-01-27 10:44:42
只需使用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");发布于 2016-01-27 10:44:36
尝尝这个
<?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");
?>发布于 2016-01-27 11:37:27
尝尝这个。
$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));https://stackoverflow.com/questions/35034716
复制相似问题