0

i have two DateTime

$datetime1 = date_create_from_format('H:i', '12:10');
$datetime2 = date_create_from_format('H:i', '18:16');

and i have interval in minutes, some for examlpe 56 minutes

How many times this interval includes between $datetime1 and $datetime2?

I try

$diff = $datetime1->diff($datetime2)

but object $diff do not include this information

4 Answers4

0
(strtotime($datetime2) - strtotime($datetime1))/56*60*60

This will give you then number of intervals in between two datetimes

0

Using DateTime you could do the following.

$datetime1 = new DateTime( '12:10' );
$datetime2 = new DateTime( '18:16' );

$diff      = $datetime1->diff( $datetime2 );

$total     = $diff->i + ( $diff->h * 60 );

echo $total;
Blinkydamo
  • 1,582
  • 9
  • 20
0

but object $diff do not include this information

It does, you just have to find it. Use the h and i attributes :

<?php
$datetime1 = date_create_from_format('H:i', '12:10');
$datetime2 = date_create_from_format('H:i', '18:16');

$diff = $datetime1->diff($datetime2);
$minutes_diff = $diff->h * 60 + $diff->i;
var_dump($minutes_diff); // int(366)

$interval = 56;
$times_interval = $minutes_diff / $interval;
var_dump($times_interval); // float(6.5357142857143)

?>
roberto06
  • 3,844
  • 1
  • 18
  • 29
0

@Taras Germanyuk From Ukraine, for things like this, DateTime is your friend: http://php.net/manual/en/dateinterval.format.php, http://php.net/manual/en/book.datetime.php. It makes these kinds of calculations easy.

<?php
$d1 = new DateTime('12:10');
$d2 = new DateTime('18:16');

$interval = $d1->diff($d2);

echo ($interval->format('%i') + ($interval->format('%h') * 60));
?>
raphael75
  • 2,982
  • 4
  • 29
  • 44