1

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.

mbunal
  • 21
  • 1
  • 5
  • what is your `date_default_timezone` ? and it should be `hours` instead of `hour` – PHP Ninja Dec 02 '19 at 12:04
  • Does this answer your question? [Date Format - How to add 'n' hours to the current time in php?](https://stackoverflow.com/questions/31779306/date-format-how-to-add-n-hours-to-the-current-time-in-php) – PHP Ninja Dec 02 '19 at 12:06
  • Does this answer your question? [Add 'x' number of hours to date](https://stackoverflow.com/questions/11386308/add-x-number-of-hours-to-date) – Dave Dec 02 '19 at 12:19
  • @Gulshan `date_default_timezone_set('Asia/Istanbul');` – mbunal Dec 02 '19 at 13:38
  • none of link my answer. – mbunal Dec 02 '19 at 13:44
  • i just discovered that it works correctly with `date_default_timezone_set('UTC')` – mbunal Dec 02 '19 at 14:22
  • http://phpfiddle.org/lite/code/32ug-7w4y please check this out. i want utc version. – mbunal Dec 02 '19 at 14:38
  • @mbunal its caused of `day light saving` here is a link which can help you to prevent this issue https://stackoverflow.com/questions/13698779/timezone-with-dst-handling-by-php – PHP Ninja Dec 03 '19 at 04:31

2 Answers2

0

http://phpfiddle.org/lite/code/931q-48kk
my simple solution:

<pre>
<?php
date_default_timezone_set('Asia/Istanbul');
$t = mt_rand(1, 2247385600);
$saat = date("H", $t);
if ($saat>=0 && $saat<6)
{
    $now = date("Y-m-d H:i:s", $t);
    $t = strtotime("-1 day", $t);
    $ilk = date("Y-m-d 18:00:00", $t);
    $t = strtotime("+5 hour 59 minute 59 second", strtotime($ilk));
    $son = date("Y-m-d H:i:s", $t);
}
elseif ($saat>=6 && $saat<12)
{
    $now = date("Y-m-d H:i:s", $t);
    $ilk = date("Y-m-d 00:00:00", $t);
    $t = strtotime("+5 hour 59 minute 59 second", strtotime($ilk));
    $son = date("Y-m-d H:i:s", $t);
}
elseif ($saat>=12 && $saat<18)
{
    $now = date("Y-m-d H:i:s", $t);
    $ilk = date("Y-m-d 06:00:00", $t);
    $t = strtotime("+5 hour 59 minute 59 second", strtotime($ilk));
    $son = date("Y-m-d H:i:s", $t);
}
else
{
    $now = date("Y-m-d H:i:s", $t);
    $ilk = date("Y-m-d 12:00:00", $t);
    $t = strtotime("+5 hour 59 minute 59 second", strtotime($ilk));
    $son = date("Y-m-d H:i:s", $t);
}
echo $now . " => " . $ilk . " - ". $son;
?>
</pre>

now, the script populate data from $ilk to $son

mbunal
  • 21
  • 1
  • 5
0

If you can use the DateTime extension dt you can do that

<?php
require '/yourpath/class.dt.php';

date_default_timezone_set("Europe/Berlin");  //or others

$dtStart = dt::create('2001-10-23 07:56:28')
  ->cut("6 hours")
  ->modify("-6 hours")
; 
echo $dtStart->format("Y-m-d H:i:s"); //2001-10-23 00:00:00

$dtEnd = dt::create('2001-10-23 07:56:28')
  ->cut("6 hours")
  ->modify("-1 Second")
; //2001-10-23 05:59:59
echo $dtEnd->format("Y-m-d H:i:s"); //2001-10-23 05:59:59
jspit
  • 7,276
  • 1
  • 9
  • 17