-1

I have for example:

$first = '2014-05-18 12:00:00';
$last = '2014-05-18 13:30:00';

and i would like calculate interval in 30 minutes for these dates. For this example interval = 3.

Other example:

$first = '2014-05-18 11:00:00';
$last = '2014-05-18 14:00:00';

interval = 6;

How is the best way for this in PHP?

brandon94
  • 13
  • 2
  • Look over to the right under "Related" >>> you might find something there. Plus, you will find many results via Google, which will most likely lead you back here on SO, from Q&A's already asked and answered. – Funk Forty Niner May 18 '14 at 19:30

2 Answers2

1

Simple math solution

$s=floor(abs(strtotime($first)-strtotime($last))/30/60);

Here is fiddle https://eval.in/153120

mleko
  • 11,650
  • 6
  • 50
  • 71
0
function timeDiff30minutes($first,$last){
    $first = new Datetime($first);
    $last = new Datetime($last);
    $time_diff = $first->diff($last);
    $time_diff_minute = $time_diff->days * 24 * 60;
    $time_diff_minute += $time_diff->h * 60;
    $time_diff_minute += $time_diff->i;
    return $time_diff_minute/30;
}
timeDiff30minutes('2014-05-18 11:00:00','2014-05-18 14:00:00') // output 6
Lewis
  • 14,132
  • 12
  • 66
  • 87