- date_default_timezone_set: documentation ( source)
- iterator_count: documentation ( source)
<?php
function display(\DateTimeInterface $start, \DateTimeInterface $end)
{
echo $start->format('Y-m-d H:i T') . ' to ' . $end->format('Y-m-d H:i T') . \PHP_EOL;
$periods = new \DatePeriod($start, new \DateInterval('PT1H'), $end);
$hours = iterator_count($periods);
echo $hours . ' hours (iterator_count)';
echo \PHP_EOL;
$diff = $end->getTimestamp() - $start->getTimestamp();
$hours = $diff / ( 60 * 60 );
echo $hours . ' hours (60 * 60)';
echo \PHP_EOL;
$diff = $end->diff($start);
$hours = $diff->h + ($diff->days * 24);
echo $hours . ' hours (days * 24)';
}
function go(\DateTimezone $tz)
{
date_default_timezone_set($tz->getName());
//DST starts Apr. 2nd 02:00 and moves to 03:00
display(
new \DateTime('2006-04-01T12:00:00'),
new \DateTime('2006-04-02T12:00:00')
);
echo \PHP_EOL . \PHP_EOL;
//DST ends Oct. 29th 02:00 and moves to 01:00
display(
new \DateTime('2006-10-28T12:00:00'),
new \DateTime('2006-10-29T12:00:00')
);
echo \PHP_EOL . \PHP_EOL;
//full year
display(
new \DateTime('2006-01-01T12:00:00'),
new \DateTime('2007-01-01T12:00:00')
);
}
go(new \DateTimezone('America/New_York'));
echo \PHP_EOL . \PHP_EOL . '---------' . \PHP_EOL . \PHP_EOL;
go(new \DateTimezone('UTC'));