0

未来时间是:2012-05-26 00:00:00

假设有三个变量:$hour $minute $second

现在,我想用未来的时间减去现在的时间。然后给左边的小时$hour,给左边的分钟$minute,给左边的秒$second

对不起,我是 php 的新手,现在我被卡住了如何进行数学运算?谢谢你

4

3 回答 3

2

一个非常好的日期和时间资源..

http://www.php.net/manual/en/function.time.php

- 这里有一些样本做类似的事情。

于 2012-05-22T02:13:09.493 回答
1

检查date_diff函数。您在那里提出的问题有确切的解决方案。

这是记录如何格式化输出的页面 (DateInterval::format) 。

$now = date_create();
// use "now" and necessary DateTimeZone in the arguments
$otherDate = date_create('2020-04-13');
$interval = date_diff($now, $futureDate);
echo $interval->format('%a days');
于 2012-05-22T02:22:07.550 回答
0

以下是时分秒差的数学运算

$future_datetime = '2012-05-26 00:00:00';
$future = strtotime($future_datetime); //future datetime in seconds
$now_datetime = date('Y-m-d H:i:s');
$now = date('U'); //now datetime in seconds

//The math for calculating the difference in hours, minutes and seconds
$difference = $future - $now;
$second = 1;
$minute = 60 * $second;
$hour = 60 * $minute;
$difference_hours = floor($difference/$hour);
$remainder = $difference - ($difference_hours * $hour);
$difference_minutes = floor($remainder/$minute);
$remainder = $remainder - ($difference_minutes * $minute);
$difference_seconds = $remainder;
echo "The difference between $future_datetime and $now_datetime is $difference_hours hours, $difference_minutes minutes and $difference_seconds seconds";
于 2012-05-22T02:17:40.070 回答