Dylan Cross
Dylan Cross

Reputation: 5986

PHP mktime with daylight savings and other variables

I use mktime() to store all of my dates for everything in my database, and I have a script that goes through and returns dates for set amounts of times, this works fine for showing how long ago things were posted, however when I try to return the exact date/time it returns one hour ahead of time, so I'm wondering how to get the date to always return the correct date when the time gets changed for any daylight savings or anything.

Here's basically what I use (cut down to only show small portion of it):

function time_stamp($session_time) 
{ 
    date_default_timezone_set("EST");

    $time_difference = time() - $session_time; 

    $seconds = $time_difference; 
    $minutes = round($time_difference / 60 );
    $hours = round($time_difference / 3600 ); 
    $days = round($time_difference / 86400 ); 
    $weeks = round($time_difference / 604800 ); 
    $months = round($time_difference / 2419200 ); 
    $years = round($time_difference / 29030400 );

    // Seconds
    if($seconds==0)
    {
         return $seconds." second ago";     //See this works fine, obviously
    }
        else if($seconds <=60&&$seconds>0)
    {
        return date('F jS \a\t g:ia ', $session_time+60*60); //it's when i use date()
                                                             //it doesn't
    }

Upvotes: 0

Views: 791

Answers (1)

Dan Kanze
Dan Kanze

Reputation: 18595

Simple solution...

Add this one line in your php.ini file:

date.timezone = "America/New_York"

After doing this, there is no need for:

date_default_timezone_set("EST");

And using functions like:

time();

Or

date();

Shouldn't be a problem.

Upvotes: 2

Related Questions