Calculate number of hours between 2 dates in PHP

前端 未结 16 1322
轮回少年
轮回少年 2020-11-22 14:07

How do I calculate the difference between two dates in hours?

For example:

day1=2006-04-12 12:30:00
day2=2006-04-14 11:30:00

In thi

16条回答
  •  醉梦人生
    2020-11-22 14:53

    To provide another method for DatePeriod when using the UTC or GMT timezone.

    Count Hours https://3v4l.org/Mu3HD

    $start = new \DateTime('2006-04-12T12:30:00');
    $end = new \DateTime('2006-04-14T11:30:00');
    
    //determine what interval should be used - can change to weeks, months, etc
    $interval = new \DateInterval('PT1H');
    
    //create periods every hour between the two dates
    $periods = new \DatePeriod($start, $interval, $end);
    
    //count the number of objects within the periods
    $hours = iterator_count($periods);
    echo $hours . ' hours'; 
    
    //difference between Unix Epoch
    $diff = $end->getTimestamp() - $start->getTimestamp();
    $hours = $diff / ( 60 * 60 );
    echo $hours . ' hours (60 * 60)';
    
    //difference between days
    $diff = $end->diff($start);
    $hours = $diff->h + ($diff->days * 24);
    echo $hours . ' hours (days * 24)';
    

    Result

    47 hours (iterator_count)
    47 hours (60 * 60)
    47 hours (days * 24)
    

    Count Hours with Daylight Savings https://3v4l.org/QBQUB

    Please be advised that DatePeriod excludes an hour for DST but does not add another hour when DST ends. So its usage is subjective to your desired outcome and date range.

    See the current bug report

    //set timezone to UTC to disregard daylight savings
    date_default_timezone_set('America/New_York');
    
    $interval = new \DateInterval('PT1H');
    
    //DST starts Apr. 2nd 02:00 and moves to 03:00
    $start = new \DateTime('2006-04-01T12:00:00');  
    $end = new \DateTime('2006-04-02T12:00:00');
    
    $periods = new \DatePeriod($start, $interval, $end);
    $hours = iterator_count($periods);
    echo $hours . ' hours';
    
    //DST ends Oct. 29th 02:00 and moves to 01:00
    $start = new \DateTime('2006-10-28T12:00:00');
    $end = new \DateTime('2006-10-29T12:00:00'); 
    
    $periods = new \DatePeriod($start, $interval, $end);
    $hours = iterator_count($periods);
    echo $hours . ' hours';
    

    Result

    #2006-04-01 12:00 EST to 2006-04-02 12:00 EDT
    23 hours (iterator_count)
    //23 hours (60 * 60)
    //24 hours (days * 24)
    
    #2006-10-28 12:00 EDT to 2006-10-29 12:00 EST
    24 hours (iterator_count)
    //25 hours (60 * 60)
    //24 hours (days * 24)
    
    #2006-01-01 12:00 EST to 2007-01-01 12:00 EST
    8759 hours (iterator_count)
    //8760 hours (60 * 60)
    //8760 hours (days * 24)
    
    //------
    
    #2006-04-01 12:00 UTC to 2006-04-02 12:00 UTC
    24 hours (iterator_count)
    //24 hours (60 * 60)
    //24 hours (days * 24)
    
    #2006-10-28 12:00 UTC to 2006-10-29 12:00 UTC
    24 hours (iterator_count)
    //24 hours (60 * 60)
    //24 hours (days * 24)
    
    #2006-01-01 12:00 UTC to 2007-01-01 12:00 UTC
    8760 hours (iterator_count)
    //8760 hours (60 * 60)
    //8760 hours (days * 24)
    

提交回复
热议问题