KutePHP
KutePHP

Reputation: 2236

One hour difference in date and time after adding Daylight Saving Time offset in php

HI All,

I'm trying to match today's date and time at Atalanta to a database value. I'm testing following code.

    $date = new DateTime();
    $newToday = $date->format('Y-m-d H:i:s');
    $dateTimeArr = split(" ",$newToday);
    $dateArr = split("-", $dateTimeArr[0]);
    $timeArr = split(":",$dateTimeArr[1]);

    $testTime = date("Y-m-d H:i",mktime($timeArr[0]+4, $timeArr[1], $timeArr[2], $dateArr[1], $dateArr[2], $dateArr[0])); // 4 is Daylight Saving Time offset

When I run the code, I found that there is 1 hour time difference if I check the time at http://www.timetemperature.com/tzga/atlanta.shtml

I'm adding the day light saving offset which 4 hours, but still the time I get is 1 hour more than the actual time. Why this difference is seen ? How to rectify this ?

EDIT

My server is at different time zone than Atalanta. I want to handle the time difference without knowing the timezones. For this, for each city we have added timezone offset in database.

Upvotes: 0

Views: 978

Answers (2)

Carlos Campderrós
Carlos Campderrós

Reputation: 22972

Use the DateTime and DateTimeZone objects:

//EDT = Eastern Daylight Saving Time
$x = new DateTime(null, new DateTimeZone('EDT')); 
echo $x->format("Y-m-d H:i")

I think that if you use America/New_York (which is in the same timezone as Atlanta) as the timezone, it will change accordingly between normal and daylight saving time.

$x = new DateTime(null, new DateTimeZone('America/New_York')); 
echo $x->format("Y-m-d H:i")

Both versions output the same time for me.

Upvotes: 2

Tudor Constantin
Tudor Constantin

Reputation: 26861

Have you checked your server's (the computer that executes the actual PHP script) time? See if it has the right time

Upvotes: 0

Related Questions