doubijiao2094 2011-03-31 13:57
浏览 33
已采纳

发现两次之间的差异。 (PHP / MySQL的)

I am trying to identify the duration of something by finding the difference between the start time and the end time.

//bring date & time input into mysql datetime format for query

$t_start_datetime = date('Y-m-d G:i:s' ,strtotime($_POST['t_date'].' '.$_POST['t_start'].' '.$_POST['s_ampm']));
$t_end_datetime = date('Y-m-d G:i:s' ,strtotime($_POST['t_date'].' '.$_POST['t_end'].' '.$_POST['e_ampm']));

// get duration value of test by finding the difference between the start and end time.
$end_time= strtotime($_POST['t_date'].' '.$_POST['t_start'].' '.$_POST['s_ampm']);
$start_time= strtotime($_POST['t_date'].' '.$_POST['t_end'].' '.$_POST['e_ampm']);

$temp = $end_time - $start_time;

echo date('G:i:s', $end_time);
echo date('G:i:s', $start_time);
//here is the duration
echo date('G:i:s', abs($temp));

So lets say the value of start_time was 6:00:00 and end_time 7:45:00 my duration is coming out as 2:45:00 :S

It's always an hour more than the actually difference.

What am I doing wrong? Please enlighten me.

Thanks

  • 写回答

4条回答 默认 最新

  • doulun7739 2011-03-31 14:05
    关注
    $end_time   = new DateTime($_POST['t_date'].' '.$_POST['t_send'].' '.$_POST['s_ampm']);
    $start_time = new DateTime($_POST['t_date'].' '.$_POST['t_start'].' '.$_POST['e_ampm']);
    $diff       = $end_time->diff($start_time);
    echo $diff->format('%H:%I:%S');
    

    You cannot use the regular date formatting here as it'll treat $temp as a UNIX timestamp and will therefore be influenced by timezones and issues such as DST, e.g.

    $e = strtotime('2011-03-31 12:30:00'); // 1301567400
    $s = strtotime('2011-03-31 10:55:00'); // 1301561700
    $d = $e - $s;                          //       5700
    echo date(DateTime::RSS, $d);
    // Thu, 01 Jan 1970 02:35:00 +0100
    echo date('G:i:s', $d);
    // 2:35:00
    

    To circumvent this problem you could use

    $d = new DateTime('@'.$d);
    echo $d->format('G:i:s');
    

    because DateTime does not take timezones into account when using it with timestamps.

    EDIT

    As the OP uses PHP 5.1.x there is another solution:

    $end_time   = strtotime($_POST['t_date'].' '.$_POST['t_start'].' '.$_POST['s_ampm']);
    $start_time = strtotime($_POST['t_date'].' '.$_POST['t_end'].' '.$_POST['e_ampm']);
    $temp       = abs($end_time - $start_time);
    echo gmdate('G:i:s', $temp);
    

    But this only works if $start_time and $end_time are within a 24 hour timespan (as do all solutions presented except for the first one).

    The manual solution (but it works in general and does not rely on any date-time-handling

    $end_time   = strtotime($_POST['t_date'].' '.$_POST['t_start'].' '.$_POST['s_ampm']);
    $start_time = strtotime($_POST['t_date'].' '.$_POST['t_end'].' '.$_POST['e_ampm']);
    $temp       = abs($end_time - $start_time);
    
    $hours   = floor($temp / 3600);
    $minutes = floor( ($temp - $hours*3600) / 60 );
    $seconds = $temp - $hours*3600 - $minutes*60;
    $diff    = sprintf('%d:%02d:%02d', $hours, $minutes, $seconds);
    

    This can easily be extended to days and weeks. Months and years will be difficult because they change their length throughout a year or throughout decades.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?