计算当前和x天之间的倒计时到一天的特定时间

时间:2012-08-24 15:20:48

标签: php time

我完全被困在这里。我试图从现在的时间到现在的下午6点的7天,得到多少天的小时和分钟。我查看从$ difference变量生成的秒数,当我将数学转换为天数小时和分钟时它是正确的但是出于某种原因我在输出语句中调用特定的天,小时和分钟时它是不正确的。我究竟做错了什么。这是代码。

<?php
date_default_timezone_set('America/New_York');
$nextWeek = strtotime('+7 days');
$m = date('n', $nextWeek);
$d = date('j', $nextWeek);
$y = date('Y', $nextWeek);
$difference = mktime(18,0,0,$m,$d,$y) - time();


echo '<p>Current date and time is' .date(' l F d, Y '). 'at '.date('g:i a').' You have an appointment in a week on '.date(' n/j/Y ', $nextWeek).' at 6pm. There are ' .date(' j ', $difference).' days, ' .date(' g ', $difference).' hours, and ' .date(' i ', $difference).' minutes until your appointment.</p>';

echo mktime(18,0,0,$m,$d,$y),"\n";
echo $difference;

?>

2 个答案:

答案 0 :(得分:0)

问题是你在一个不代表 date 的数字上使用PHP的date()函数。您的变量$difference表示两个日期之间的差异,以秒为单位。要获得正确的输出,您应该编写自己的函数将这些秒转换为天数,小时数,分钟数等。

它可能看起来像这样:

function getTimeText($seconds)
{
    $return = array();

    $return["days"] = floor($seconds/86400); // 86400 seconds in a day
    $seconds -= ($return["days"]*86400);

    $return["hours"] = floor($seconds/3600); // 3600 seconds in an hour
    $seconds -= ($return["hours"]*3600);

    $return["minutes"] = floor($seconds/60); // 60 seconds in a minute

    return $return;
}

答案 1 :(得分:0)

尝试查看this。页面下方的示例向您显示如何查找两个日期之间的差异。您应该可以通过更改格式使用它来返回当前时间与7:00之间的时间之间的差异。

相关问题