1

My linux box is set to use UTC. Given a timezone and a date, I want to get the date range so that I can query the database for records created on any given day. For instance, if it is now 2018-03-24 at 9am in America/Denver timezone. I want to get the start and end times for this date in UTC. How can I get the UTC equivilant of the beginning of that date?

<?php
$date = new DateTime(date('Y-m-d H:i:s'), new     DateTimeZone('America/Denver'));
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d 00:00:00');
?>

this returns 2018-03-24 00:00:00 which is not correct. Any pointers?

Steve Lloyd
  • 773
  • 2
  • 12
  • 33

2 Answers2

1

Try using this function.

 function UTCTimeToLocalTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
    if ($tz == '')
        $tz = date_default_timezone_get();

    $utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
        DateTimeZone('UTC'));
    $local_datetime = $utc_datetime;

    $local_datetime->setTimeZone(new DateTimeZone($tz));
    return $local_datetime->format($ToDateFormat);
}

echo UTCTimeToLocalTime('2015-07-01 13:30:00','America/Denver');



function LocalTimeToUTCTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
    if ($tz == '')
        $tz = date_default_timezone_get();
    $utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
        DateTimeZone($tz));
    $local_datetime = $utc_datetime;
    $local_datetime->setTimeZone(new DateTimeZone('UTC'));
    return $local_datetime->format($ToDateFormat);
}
srp
  • 560
  • 1
  • 5
  • 14
  • Works like a charm. Thank you! – Steve Lloyd Mar 24 '18 at 16:10
  • I believe the function name should be called LocalTimeToUTC though. If I pass it a local time (2018-03-24 00:00:00) it will return the UTC time, right? 2018-03-23 18:00:00 – Steve Lloyd Mar 24 '18 at 16:14
  • I need to get the datetime range of any given date. If today is 2018-03-24 in Americal/Denver time I need the UTC times back so I can query the database for this dates records. In UTC this would need to be 2018-03-23 18:00:00 to 2018-03-24 17:59:59. – Steve Lloyd Mar 24 '18 at 16:21
  • 1
    i have edited my answer and use the local to utc function – srp Mar 24 '18 at 16:22
  • If I pass in 2018-03-24 00:00:00 to your new function it returns 2018-03-24 06:00:00 the first function actually gives me back the correct UTC time of 2018-03-23 18:00:00 – Steve Lloyd Mar 24 '18 at 16:45
  • The function names need to be switched in order for them to work as expected. – Steve Lloyd Mar 24 '18 at 21:48
0

You feed the DateTime constructor with a bogus local time:

new DateTime(date('Y-m-d H:i:s'), newDateTimeZone('America/Denver'));
             ^^^^

You tell PHP that's a Denver local time but you don't really know. Since the string does not contain time zone information PHP will use the default time zone.

Just drop date(). It serves no purpose and only makes things harder.

<?php
$date = new DateTime('now', new DateTimeZone('America/Denver'));
echo $date->format('r'), PHP_EOL;
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('r'), PHP_EOL;
Sun, 25 Mar 2018 06:11:21 -0600
Sun, 25 Mar 2018 12:11:21 +0000
Álvaro González
  • 142,137
  • 41
  • 261
  • 360