Search code examples
phpdatedatetimetimedateinterval

DateTime + 12 hours showing same DateTime. Why?


When I am increasing DateTime value by any hours, the result is OKAY, but when I increase it by 12 hours, it is not increasing.


Please see the following code for details:



$creation_date = new DateTime('2016-09-07 06:00:00', new DateTimeZone('Asia/Kolkata'));
$expiration_date = new DateTime('2016-09-07 06:00:00', new DateTimeZone('Asia/Kolkata'));

When I increase the $expiration_date variable by 1 hour, 3 hour, 8 hour, 24 hours etc., the result is perfect. For example,

Case 1:

$expiration_date->add(new DateInterval('PT1H'));
echo "Creation Date: ".$creation_date->format('Asia/Kolkata')."<br/>Expiration Date: ".$expiration_date->format('Asia/Kolkata');

Result 1:

Creation Date: 2016-09-07 06:00:00
Expiration Date: 2016-09-07 07:00:00

Case 2:

$expiration_date->add(new DateInterval('PT3H'));
echo "Creation Date: ".$creation_date->format('Asia/Kolkata')."<br/>Expiration Date: ".$expiration_date->format('Asia/Kolkata');

Result 2:
Creation Date: 2016-09-07 06:00:00
Expiration Date: 2016-09-07 09:00:00

Case 3:

$expiration_date->add(new DateInterval('PT8H'));
echo "Creation Date: ".$creation_date->format('Asia/Kolkata')."<br/>Expiration Date: ".$expiration_date->format('Asia/Kolkata');

Result 3:
Creation Date: 2016-09-07 06:00:00
Expiration Date: 2016-09-07 02:00:00

Case 4:

$expiration_date->add(new DateInterval('PT24H'));
echo "Creation Date: ".$creation_date->format('Asia/Kolkata')."<br/>Expiration Date: ".$expiration_date->format('Asia/Kolkata');

Result 4:
Creation Date: 2016-09-07 06:00:00
Expiration Date: 2016-09-08 06:00:00

But when I increase the $expiration_date variable by 12 hours, the date is not getting increased!

They are showing the same datetime!

Case 5:

$expiration_date->add(new DateInterval('PT12H'));
echo "Creation Date: ".$creation_date->format('Asia/Kolkata')."<br/>Expiration Date: ".$expiration_date->format('Asia/Kolkata');

Result 5:
Creation Date: 2016-09-07 06:00:00
Expiration Date: 2016-09-07 06:00:00



What am I doing wrong?


Solution

  • 8 hours gives you

    Creation Date: 2016-09-07 06:00:00
    Expiration Date: 2016-09-07 02:00:00
    

    Do you really think that that 6 + 8 is 2 ? No, it is 14 which is 2pm The same with 6 + 12 is 18 which is 6pm. Change format of displayed data to 24 hours ;)