1

I got two timestamps:

$date1 = new DateTime("2017-08-02T08:00:00.000Z");
$date2 = new DateTime("2017-10-02T17:00:00.000Z");

I need to know how many hours were between those two, between 08:00 to 17:00.

It should return 24h, 3 full work days.

Pang
  • 9,564
  • 146
  • 81
  • 122
A.Krop
  • 11
  • 2
  • 1
    StackOverflow expects you to [**try to solve your own problem**](http://meta.stackoverflow.com/questions/261592) before asking for help, and part of doing that is [**searching**](http://stackoverflow.com/search) for similar questions. It took mere seconds to find the exact answer to your question - probably less time than writing it. Search first in future to save yourself time :) For further information, please refer to the help article regarding [**how to ask good questions**](http://stackoverflow.com/help/how-to-ask), and take the [**tour of the site**](http://stackoverflow.com/tour) :) – Obsidian Age Aug 09 '17 at 03:35

3 Answers3

1
<?php
$date1 = new DateTime("2017-08-02T10:19:55.022Z");
$date2 = new DateTime("2017-10-02T10:19:55.022Z");
$interval = $date1 ->diff($date2 );
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
?>
  • ty for answer, but at this point it returns me 0, it should be about ~19h :/ im trieing to count how many work hours is between those 2 dates. – A.Krop Aug 09 '17 at 03:39
0

Get the interval of these two dates;

$date1 = new DateTime("2017-08-02T10:19:55.022Z");
$date2 = new DateTime("2017-10-02T10:19:55.022Z");
$diff = $date1 ->diff($date2);

$diff is an instance of DateInterval Like below:

DateInterval {#727
     +"y": 0,
     +"m": 2,
     +"d": 0,
     +"h": 0,
     +"i": 0,
     +"s": 0,
     +"weekday": 0,
     +"weekday_behavior": 0,
     +"first_last_day_of": 0,
     +"invert": 0,
     +"days": 61,
     +"special_type": 0,
     +"special_amount": 0,
     +"have_weekday_relative": 0,
     +"have_special_relative": 0,
   }

So, $diff->d is the part of day interval $diff->h is the part of hour interval. You can use these properties to get what you want.

More details of class DateInterval, please checkout this official documentation: http://php.net/manual/en/class.dateinterval.php

Kevin Yan
  • 1,236
  • 11
  • 19
0
<?php
$date1 = new DateTime("2017-08-02T10:19:55.022Z");
$date2 = new DateTime("2017-10-02T10:19:55.022Z");
$interval = $date1->getTimestamp() -$date2->getTimestamp();
echo abs($interval/(60*60));
?>
  • While this might answer the question, please add a short description of what your command does and why it solves the initial problem. – user1438038 Aug 09 '17 at 07:11