0

我想从今天的日期中减去未来的日期,我希望它显示目标日期之前的日期、小时和分钟。

这是我的代码

<?php

date_default_timezone_set('Europe/London');

$format = "h:i d";
$date = date($format);
$target = date($format, mktime(0,0,0,12,8,2011));

echo date($format, $target-$date);

?>

亲切的问候,亚当

4

2 回答 2

5

Don't use date for operations, it's meant for displaying dates. Instead subtract 2 timestamps:

...
$date = mktime(now...);
$target = mktime(0,0,0,12,8,2011);

echo date($format, $target - $date);

But you have to realize timestamps start in 1970 and end in 2038, therefore, for example, 2011 - 2007 = 1974.

More suitable in your case would be date_diff as @Kerrek SB suggested in comment.

Example (from php.net):

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days'); // +2 days
于 2011-12-01T13:56:50.627 回答
1

也许这会帮助你

<?php

$target = mktime (23, 34, 0, 9, 28, 2009);

$today = mktime();

$difference =($target-$today) ;

$days = $difference / 3600 / 24;
$difference = $difference - floor($days) * 3600 * 24;
$hours = $difference / 3600;
$difference = $difference - floor($hours) * 3600;
$minutes = $difference / 60;
echo "Days: ";
echo floor($days);
echo "<br />";
echo "Hours: ";
echo floor($hours);
echo "<br />";
echo "Minutes: ";
echo floor($minutes);

?>
于 2011-12-01T14:16:48.023 回答