Having unexpected results when using strtotime with timezone and calculations. My PHP is set to UTC. strtotime("-6 days 00:00:00 America/Chicago"); seems to be giving unexpected results. I thought by providing the timezone it would be 'calculated IN' that timezone, but that doesn't appear to the case.
In my example I expect the results to be the same, regardless of PHP's default timezone because 'America/Chicago' is used in strtotime. Instead it seems to take the current day (according to default timezone) and subtract days then calculate 00:00:00 in the timezone supplied.
Am I formatting this wrong?
I apologize for the messy code, but it is just an example demonstrating what I am talking about.
date_default_timezone_set("UTC");
echo date_default_timezone_get();
echo "</br>";
echo "</br>";
echo convertTS(time(), "America/Chicago");
echo "</br>";
echo "</br>";
$test = strtotime("-6 days 00:00:00 America/Chicago");
echo $test;
echo "</br>";
echo convertTS($test, "America/Chicago");
echo "</br>";
$test = strtotime("-7 days 00:00:00 America/Chicago");
echo $test;
echo "</br>";
echo convertTS($test, "America/Chicago");
echo "</br>";
echo "</br>";
echo "-----------------";
echo "</br>";
echo "</br>";
///now in America/Chicago
date_default_timezone_set("America/Chicago");
echo date_default_timezone_get();
echo "</br>";
echo "</br>";
echo convertTS(time(), "America/Chicago");
echo "</br>";
echo "</br>";
$test = strtotime("-6 days 00:00:00 America/Chicago");
echo $test;
echo "</br>";
echo convertTS($test, "America/Chicago");
echo "</br>";
$test = strtotime("-7 days 00:00:00 America/Chicago");
echo $test;
echo "</br>";
echo convertTS($test, "America/Chicago");
function convertTS($timestamp, $timezone)
{
if( empty($timestamp) )
{
return 'n/a';
}
else
{
//output the time
$dt = new DateTime('@'.$timestamp);
$dt->setTimeZone(new DateTimeZone($timezone));
return $dt->format('D, m/d/y @ g:i:s a T');
}
}
result of the above at the time I ran it :
UTC
Fri, 01/04/19 @ 11:39:15 pm CST
1546149600
Sun, 12/30/18 @ 12:00:00 am CST
1546063200
Sat, 12/29/18 @ 12:00:00 am CST
-----------------
America/Chicago
Fri, 01/04/19 @ 11:39:15 pm CST
1546063200
Sat, 12/29/18 @ 12:00:00 am CST
1545976800
Fri, 12/28/18 @ 12:00:00 am CST
I made a sandbox to recreate this issue at all times here. The difference being a timestamp is provided rather than using time() to show how the default timezone affects the results depending on time of the current day.

