note 123402 added to function.strtotime
| From: | mirco dot babin at gmail dot com | Date: | Thu, 06 Dec 2018 09:10:22 +0000 |
| Subject: | note 123402 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209855@lists.php.net to get a copy of this message | ||
One important thing you should remember is that the timestamp value returned by time() is time-zone
agnostic and gets the number of seconds since 1 January 1970 at 00:00:00 UTC. This means that at a
particular point in time, this function will return the same value in the US, Europe, India, Japan,
...
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;
}
?>
Test:
<?php
$tz = 'Europe/Amsterdam';
$utctime = '2018-12-06T09:04:55';
if (!date_default_timezone_set($tz)) {
WriteLine('Setting default timezone to ' . $tz . ' failed');
die;
}
WriteLine();
WriteLine('Default timezone set to ' . $tz);
WriteLine('UTC time: ' . $utctime);
WriteLine();
WriteLine('[ UTCdatestringToTime ]');
$phptime = UTCdatestringToTime($utctime);
WriteLine('PHP time: ' . $phptime);
WriteTime($phptime, true);
WriteTime($phptime, false);
WriteLine();
WriteLine('-------------------------------------------------------------------------------');
WriteLine('[ strtotime($utctime) - Converts $utctime as if it was in timezone ' . $tz .
' because the string has no timezone specification ]');
$phptime = strtotime($utctime); //Seconds since the unix epoch
WriteLine('PHP time: ' . $phptime);
WriteTime($phptime, true);
WriteTime($phptime, false);
function WriteLine($text = '')
{
echo $text . "\r\n";
}
function WriteTime($time, bool $asUTC)
{
$tz = date_default_timezone_get();
if ($asUTC) date_default_timezone_set('UTC');
WriteLine('--> (' . date_default_timezone_get() . ') ' . date('Y-m-d
H:i:s', $time));
if ($asUTC) date_default_timezone_set($tz);
}
?>
Test output:
<?php
Default timezone set to Europe/Amsterdam
UTC time: 2018-12-06T09:04:55
[ UTCdatestringToTime ]
PHP time: 1544087095
--> (UTC) 2018-12-06 09:04:55
--> (Europe/Amsterdam) 2018-12-06 10:04:55
-------------------------------------------------------------------------------
[ strtotime($utctime) - Converts $utctime as if it was in timezone Europe/Amsterdam because the
string has no timezone specification ]
PHP time: 1544083495
--> (UTC) 2018-12-06 08:04:55
--> (Europe/Amsterdam) 2018-12-06 09:04:55
*/
?>
----
Server IP: 208.43.231.9
Probable Submitter: 84.53.107.126
----
Manual Page -- http://php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/123402
Del: integrated -- https://master.php.net/note/delete/123402/integrated
Del: useless -- https://master.php.net/note/delete/123402/useless
Del: bad code -- https://master.php.net/note/delete/123402/bad+code
Del: spam -- https://master.php.net/note/delete/123402/spam
Del: non-english -- https://master.php.net/note/delete/123402/non-english
Del: in docs -- https://master.php.net/note/delete/123402/in+docs
Del: other reasons-- https://master.php.net/note/delete/123402
Reject -- https://master.php.net/note/reject/123402
Search -- https://master.php.net/manage/user-notes.php