i want to some time calculation with this code:
$timestamp = mt_rand(1, 2147385600);
$oldDate = date("Y-m-d H:i:s", $timestamp);
$date = date("Y-m-d H:i:s", strtotime("-6 hour", (floor(strtotime($oldDate) / 6 / 60 / 60) * 6 * 60 * 60)));
echo $oldDate . " - " . $date;
i want to calculate exact previous time period (6 hours) of given time not included itself. for example:
2001-10-23 07:56:28 -> 2001-10-23 00:00:00 to 2001-10-23 05:59:59
2037-01-31 01:31:25 -> 2037-01-30 18:00:00 to 2037-01-30 23:59:59
this code working very well. but when i added date_default_timezone_set
, result becomes wrong.
how does date_default_timezone_set
break the calculations? how can i solve this?
i added a link with working and non working sample. pleasse check this out:
http://phpfiddle.org/lite/code/32ug-7w4y please click run-f9
a few times.