Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 259069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:13:59+00:00 2026-05-11T22:13:59+00:00

My server time is in GMT, and I do the following whenever someone comes

  • 0

My server time is in GMT, and I do the following whenever someone comes online.

// Set a default timezone
$defaultTimeZone = 'America/Toronto';

// load the user, if they are online...
$onlineUser = new user();
if (isset($_SESSION['user_id']))
{
    if ($onlineUser->loadUser($_SESSION['user_id']))
    {
        $defaultTimeZone = $onlineUser->timeZone;
    }
}

// set time zones
date_default_timezone_set($defaultTimeZone);
$db->query("SET SESSION time_zone = '".$defaultTimeZone."'");

So, my problem is this… whenever someone does something it stores the date/time in the users local time… causing me a whole wack of issues.

All I want is for everything to be stored in GMT, but have the users see & interact with the data in their local time zone.

EDIT:

Here is how I am updating the users status:

public function updateStatus()
{
    global $db, $common, $config;

    // update the user record
    $data = array(
        'date_last_active'  => new Zend_Db_Expr('NOW()')
    );

    $db->update('users', $data, 'user_id='.$this->userId);
}

Here is my function to turn a date stamp into seconds…

public function dateTimeToUnixTime($dateString)
{
    if (strlen($dateString) < 10)
    {
        return "";
    }

    $parseDateTime = split(" ", $dateString);
    $parseDate = split("-", $parseDateTime[0]);

    if (isset($parseDateTime[1]))
    {
        $parseTime = split(":", $parseDateTime[1]);
    }
    else
    {
        $parseTime = split(":", "00:00:00");
    }

    return mktime($parseTime[0], $parseTime[1], $parseTime[2], $parseDate[1], $parseDate[2], $parseDate[0]);
}

And finally, how I am comparing the dates:

    $date = $oUser->dateLastActive;

    $lastSeen = abs($common->dateTimeToUnixTime(date('Y-m-d H:i:s')) - $common->dateTimeToUnixTime($date));

    if ($lastSeen < 300 )
    {
        echo "<font size='1' color='green'><strong>Online</strong></font>";
    }

    if ($lastSeen >= 300)
    {
        echo "<font size='1' color='black'><strong>Offline</strong></font>";
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:14:00+00:00Added an answer on May 11, 2026 at 10:14 pm

    The trick here is to know what column type date_last_active is. I’ve had enough experience with TIMESTAMP and DATETIME to know that one translates (and honors) the MySQL time_zone session variable, the other doesn’t.

    mysql> SET SESSION time_zone = 'America/New_York';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> create table timetest ( dt datetime NULL, ts timestamp NOT NULL DEFAULT 0 );
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> INSERT INTO timetest ( dt, ts ) VALUES ( UTC_TIMESTAMP(), UTC_TIMESTAMP() );
    Query OK, 1 row affected (0.00 sec)
    
    mysql> INSERT INTO timetest ( dt, ts ) VALUES ( NOW(), NOW() );
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SELECT * FROM timetest;
    +---------------------+---------------------+
    | dt                  | ts                  |
    +---------------------+---------------------+
    | 2009-06-27 17:53:51 | 2009-06-27 17:53:51 | 
    | 2009-06-27 13:53:54 | 2009-06-27 13:53:54 | 
    +---------------------+---------------------+
    2 rows in set (0.00 sec)
    
    mysql> set session time_zone='UTC';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SELECT * FROM timetest;
    +---------------------+---------------------+
    | dt                  | ts                  |
    +---------------------+---------------------+
    | 2009-06-27 17:53:51 | 2009-06-27 21:53:51 | 
    | 2009-06-27 13:53:54 | 2009-06-27 17:53:54 | 
    +---------------------+---------------------+
    2 rows in set (0.00 sec)
    

    So, here is what I do:

    • When inserting into the database, use UTC in PHP and in MySQL. In my.cnf I have: time_zone=UTC to avoid any issues in MySQL, and in PHP I date_default_timezone_set('UTC').
    • Use UTC_TIMESTAMP() instead of NOW() – one is UTC, one uses the local (session) time zone. If you’re following the first point, above, use UTC_TIMESTAMP().
    • When selecting to display to the user, set time_zone='local_time_zone' before displaying anything.
    • If you have to display stuff, then update, make sure to bracket your calls with the appropriate timezone changes in PHP and MySQL.

    Hopefully that’s enough to figure out how to approach this. Let me know if you have further questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running on a Win2003 server, TimeZone set to (GMT -06:00) Central Time.
Our server is set to GMT time so the hour does not keep jumping
I need to return server time from a webservice. I've got the following code:
when I use the 'date' command, our server gives the GMT time. But my
I receive from my server (MST timezone) and i convert it to GMT but
My server sends me time zones of the format America/Los Angeles. On the client,
My defaulst timezone in google calendar is (GMT-08:00) Pacific Time. Now i generate a
Possible Duplicate: Daylight saving time and Timezone best practices Basically I have the following
That's basically it, I need the server local time instead of GMT, so I
My MySQL is storing date time stamps in GMT, though server's time is in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.