I would like check how iteration is between two dates with interval 30 minutes.
I have for example:
$one = new DateTime('2012-01-20 06:00');
$two = new DateTime('2012-01-20 17:30');
$first = $one->format('H:i');
$second = $two->format('H:i');
$interval = 30;
In this example $iteration = 23, but how can i calculate this?
You divide the amount of minutes between the datetimes with the desired interval.
$one = strtotime('2012-01-20 06:00');
$two = strtotime('2012-01-20 17:30');
$interval = 30;
echo round(($two - $one) / ($interval * 60));
(I've taken a shortcut and am divided the amount of seconds by amount of seconds in 30 minutes)
You might be better off using Unix time which is just the number of seconds since January 1st, 1970.
$now = date("U");
// In half an hour:
$future = $now + (30 * 60);
// (minutes * seconds in a minute)
$diff = $now - $future;
echo ($diff / 60);
// returns 30
This is essentially the same as Tatu Ulmanen's answer, but using the DateTime class as you’ve done.
$one = new DateTime('2012-01-20 06:00');
$two = new DateTime('2012-01-20 17:30');
$first = $one->getTimestamp();
$second = $two->getTimestamp();
$interval = 30;
echo round(($second - $first) / ($interval * 60));