0

I have to count the hours between two datetimes in php. These two datetimes are coming with minutes and also seconds but they can be ignored. The goal is get the sum of hours. This is sum represents a gap where problem occurs.

Some piece of code you can find down this writing. I have to know the following:

  1. do i have it done right - or are there some problem using functions like date->diff (like some months do have 30 days while others have 31 days, or problem with leap years?)
  2. is there a more simplier way to get the sum of hours by date->diff?

To achieve this goal i have done this by:

  //delete minutes and seconds from datetime object
  $datetime_start = date("Y-m-d h", strtotime($datetime_start));
  $datetime_end = date("Y-m-d h", strtotime($datetime_end));
  $datetime_start = $datetime_start.":00:00";
  $datetime_end = $datetime_end.":00:00";

  //create DateTime object
  $datetime_start = new DateTime($datetime_start);
  $datetime_end = new DateTime($datetime_end);

  //get interval between two dates and fill array      
  $interval = $datetime_end->diff($datetime_start);
  $details = array_intersect_key((array)$interval,array_flip(['y','m','d','h','i','s']));

  $sum_of_hours = 0;

  if(!empty($details["y"])){
    $sum_of_hours = $sum_of_hours + $details["y"] * 365 * 24;
  };

  if(!empty($details["m"])){
    $sum_of_hours = $sum_of_hours + $details["m"] * 30 * 24;
  };

  if(!empty($details["d"])){
    $sum_of_hours = $sum_of_hours + $details["d"] * 24;
  };

  if(!empty($details["h"])){
    $sum_of_hours = $sum_of_hours + $details["h"];
  };

Example: Between these dates "2018-06-30 02:00:05" and "2018-07-01 05:00:10" i have an interval of 1 day and 3 hours resulting in 27hours.

thx for helping

iainn
  • 16,826
  • 9
  • 33
  • 40
user3882511
  • 125
  • 1
  • 10
  • sidenote: why are you creating the $datetime_start so complicated over 4 steps? Would be much easier via [DateTime::createFromFormat](http://php.net/manual/en/datetime.createfromformat.php) – Jeff Jul 30 '18 at 14:50
  • because i have to delete (modify) minutes and seconds from start and end date... – user3882511 Jul 30 '18 at 16:28
  • could still be done in one line: `$start = DateTime::createFromFormat("Y-m-d H:i:s", "2018-06-30 02:52:05")->setTime(2,0,0);` – Jeff Jul 30 '18 at 18:05
  • ->setTime(2,0,0) overwrites my my hour. I only want to overwrite minutes and seconds with 00. Is there a way? – user3882511 Jul 31 '18 at 18:27
  • you could get the hour and set it. Unfortunately there is no way (I could find) to only set minutes and seconds. – Jeff Jul 31 '18 at 19:36
  • _I found a way!_ -> `$start = DateTime::createFromFormat("Y-m-d H:??:??", "2018-06-30 02:52:05");` - the `??` will just ignore the values and set minutes & seconds to 0 – Jeff Jul 31 '18 at 19:39
  • thx! now i can delete some lines of code.... – user3882511 Aug 01 '18 at 18:41

2 Answers2

0

You could convert them to unix timestamps and then subtract them from each other to get the seconds difference, then divide by 60 * 60 to get the hours:

floor(abs($datetime_end->getTimestamp() - $datetime_start->getTimestamp()) / (60 * 60))

This returns 27 for your scenario.

eval.in demo

Ethan
  • 4,295
  • 4
  • 25
  • 44
0
<?php
$datetime1 = date_create('2018-06-30 02:00:05');
$datetime2 = date_create('2018-07-01 05:00:10');
$interval = date_diff($datetime1, $datetime2);

$hours = ($interval->days * 24) + $interval->h; // days to hour diff

echo $hours;

This outputs 27.. for example

AnTrakS
  • 733
  • 4
  • 18