doushi1900 2015-11-21 03:53
浏览 55
已采纳

日期之间的间隔包括小时php

I want to find interval between dates, the hour function does not work correctly the hour before the time changes. I know there is an answer that partially answers this question here. But it does not answer it according to hours.

I have the following php code:

//helper function to pluralize words
function pluralize( $count, $text ){ 
    return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}

/**
 * Difference of dates
 * @param type $date1 DateTime first date
 * @param type $date2 DateTime second date
 * @return type string year, month, day, hour, minute, second 
 */
function timeDiff($date1, $date2 ){
    $string = "";  
    $interval =$date1->diff($date2);
    $suffix = ( $interval->invert ? ' ago' : '' );
    if ( $v = $interval->y >= 1 ) $string .= pluralize( $interval->y, 'year' ). ', ';
    if ( $v = $interval->m >= 1 ) $string .= pluralize( $interval->m, 'month' ). ', ';
    if ( $v = $interval->d >= 1 ) $string .= pluralize( $interval->d, 'day' ). ', ';
    if ( $v = $interval->h >= 1 ) $string .= pluralize( ($interval->h), 'hour' ) . ', ';
    if ( $v = $interval->i >= 1 ) $string .= pluralize( $interval->i, 'minute' ). ', ';
    if ( $v = $interval->i >= 1 ) $string .= pluralize( $interval->s, 'second' ). ' ';
    return $string . $suffix;
}

I want the following tests to pass.

$date1 = new DateTime("2014-05-10 20:00:00");
$date2 = new DateTime("2015-11-20 19:45:00");

//This should produce "1 year, 6 months, 9 days, 23 hours, 45 minutes"
//But it does produce "1 year, 6 months, 10 days, 45 minutes"
timeDiff($date1, $date2); 

$date3 = new DateTime("2015-11-20 20:00:00");

//This should, and does produce "1 year, 6 months, 10 days"
timeDiff($date1, $date3); 

This looks like a version issue pointed out in comments by @John Conde. It is fixed in the new version of PHP 5.4.24 and on. How can I fix this for previous version of PHP?

  • 写回答

1条回答 默认 最新

  • dousao8152 2015-11-21 17:27
    关注

    I guess your requirement is to display the elapsed time between two DateTime data items represented in your local time. (If that is not your requirement, please update your question).

    I also guess your DateTime items are created correctly in the local time zone. You can check the local timezone setting with date_default_timezone_get() and set it with date_default_timezone_set('Australia/Sydney') for example.

    It's possible the person who operates your server has set the server default timezone incorrectly, or that it's located in some other time zone from you. It's also possible that it's set to UTC (also known as Zulu or Z time, formerly known as Greenwich Mean Time or GMT). In that case you'll need to set the local timezone explicitly in your program.

    Once you have your timezone set correctly, create your DateTime items. Then convert both DateTime items to UTC. Then compute the difference. Something like this ought to do the trick.

     $utczone = new DateTimeZone('UTC');
     timeDiff($date1->setTimezone($utczone), $date2->setTimezone($utczone));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式