0

For example

strtotime("2018-12-06T09:04:55");
strtotime("2021-07-09T14:09:47.529751-04:00");

I read in the php manual that ISO dates should be avoided when using strtotime, why ? Should I extract date time from the string before using strtotime.

strtotime() will convert a string WITHOUT a timezone indication as if the string is a time in the default timezone ( date_default_timezone_set() ). So converting a UTC time like '2018-12-06T09:04:55' with strtotime() actually yields a wrong result. In this case use:

<?php
function UTCdatestringToTime($utcdatestring)
{
    $tz = date_default_timezone_get();
    date_default_timezone_set('UTC');

    $result = strtotime($utcdatestring);

    date_default_timezone_set($tz);
    return $result;
}
?>
  • Which manual are you referring to? The only warning I see in the [docs](https://www.php.net/manual/en/function.strtotime.php) is this one: "The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable." – Code4R7 Jul 01 '21 at 13:53
  • https://www.php.net/manual/en/function.strtotime.php – user16357892 Jul 03 '21 at 04:52
  • All I read is "To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible." and the top post which dates 10 years back says you should be using dots `.` between date components because strtotime() is 'smart' by design. Which reminds us to [EWD 340](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD03xx/EWD340.html). – Code4R7 Jul 04 '21 at 06:25

1 Answers1

0

If the date string contains a time zone, strtotime also takes this into account.

$strDate = "2018-12-06T09:04:55 UTC";
$ts = strtotime($strDate);  // int(1544087095)

If the time zone is missing in the date string, the default time zone is used. My Timezone is "Europe/Berlin".

$strDate = "2018-12-06T09:04:55";
$ts = strtotime($strDate);  // int(1544083495)

As a result, we get a different timestamp.

If I want to convert a date string from another time zone into a time stamp, then the best solution is to do it with the DateTime object. There I can enter the correct time zone in the 2nd parameter when creating the object.

$strDate = "2018-12-06T09:04:55";
$dt = new DateTime($strDate, new DateTimeZone('UTC'));
$ts = $dt->getTimeStamp();  // int(1544087095)

Important: If the date string contains a valid time zone, this has priority over the 2nd parameter.

$strDate = "2018-12-06T09:04:55 UTC";
$dt = new DateTime($strDate, new DateTimeZone('Europe/Berlin'));
/*
DateTime::__set_state(array(
   'date' => "2018-12-06 09:04:55.000000",
   'timezone_type' => 3,
   'timezone' => "UTC",
))
*/

The DateTimeZone ('Europe/Berlin') is ignored here.

Since strtotime also accepts a time zone in the date string, the time zone can also be added with a string concatenation.

$strDate = "2018-12-06T09:04:55";
$ts = strtotime($strDate." UTC");  //int(1544087095)

The UTCdatestringToTime function does the same. However, it is not nice to temporarily change the default time zone in the PHP script.

jspit
  • 7,276
  • 1
  • 9
  • 17