0

This may sound very unlikely to the other question.

I have two hour range.

$start = 18:00;
$end = 2:00;

the result 8 hours.

sample two.

$start = 17:30;
$end = 2:00; 

result 7.5 Hours.

How can I get this on PHP code.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
freddy
  • 155
  • 9

1 Answers1

3

Object oriented style:

<?php
$datetime1 = new DateTime('2015-04-13 18:00');
$datetime2 = new DateTime('2015-04-14 02:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%H:%I hours');
?>

Procedural Style:

<?php
$datetime1 = date_create('2015-04-13 17:30');
$datetime2 = date_create('2015-04-14 02:00');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%H:%I hours');
?>

You can read more at:

http://php.net/manual/en/datetime.diff.php

http://php.net/manual/en/dateinterval.format.php

http://php.net/manual/en/class.dateinterval.php

Community
  • 1
  • 1
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • That's a bit dangerous use for `DateTime`: it was not designed for time arithmetics (hence the name - **date**time) in particular so you may have issues with summer time. – zerkms Apr 14 '15 at 03:45
  • 1
    @zerkms which function you recommended for these cases? – Adrian Cid Almaguer Apr 14 '15 at 03:48
  • For this particular task I would calculate it manually, it's trivial: searching a decent one would take more time. – zerkms Apr 14 '15 at 03:51
  • 1
    @zerkms I wouldn't say it's dangerous, rather it's specifically designed to solve this sort of thing. It's important to note that if you switch from EDT to EST overnight, then the interval actually should be off by an hour. The right approach is to either manage all your times in UTC, or make sure you explicitly load the second DateTime with the DateTimeZone from the first. – Josh from Qaribou Apr 14 '15 at 03:51
  • @JoshfromQaribou you can only reason about EST/EDT when you have date fraction. Otherwise `DateTime` behaviour is unpredictable, since it assumes "today". "The right approach is to either manage all your times in UTC" --- the question is about time of the day, which has nothing to do with timezones. – zerkms Apr 14 '15 at 03:56
  • You'd need to compare the two dates, and see if the second one is in the past, advance it a day then. – Josh from Qaribou Apr 14 '15 at 03:57
  • @JoshfromQaribou the question is not about dates. It's only about time of the day. – zerkms Apr 14 '15 at 03:58
  • The current provided solution **will fail** if the start time before changing to/from summer time, and end time after. Hence it is dangerous for anyone to use it in production. – zerkms Apr 14 '15 at 03:59
  • @zerkms: yes, it will fail ([demo](https://eval.in/313103)), that's why you set timezone to UTC, and then there will not be any changing to/from summer time ([demo](https://eval.in/313104)). If this is not what you mean, please elaborate with example. – Glavić Apr 14 '15 at 05:57
  • @Glavić well, technically your solution will work, I agree. Practically I cannot understand why when you need to work with `time` you use `datetime` instead. I personally prefer to work with types precisely, not find hackish workarounds. – zerkms Apr 14 '15 at 06:41
  • @zerkms: I agree with you, but OP question is kinda bogus, since he is using relative times (start=18, end=2) and not saying anything about dates, so I presume he has them (based on difference=8hours). If time were absolute, then I would not recommend using DateTime. – Glavić Apr 14 '15 at 06:50