Hi guys. I hope you can help me with these please... I'm completely stumped

We use a timezone selection for locale, and I want to further use that to basically (when someone is logged in) to show all the website dates in their own time. This includes dates stored in our MySQL database.

The timezone selections are at http://pastebin.com/mf714dfa (too large to post here), these are the values that are in our timezone field in our user table

So, that's fine...

In my functions.php, included by all files I've tried the following:

if (isset($_SESSION['logged'])) {
  $search=mysql_fetch_array(mysql_query("SELECT * FROM user WHERE account='".mysql_real_escape_string($_SESSION['account'])."'"));
  $timezone=$search['timezone'];
  date_default_timezone_set($timezone);
}

It changes normal date()'s, but NOT when it's combined with strtotime. So basically, none of our database dates are being changed, which we use like:

date('M d H:i:s Y', strtotime($row['dateposted']))

And also (to rule out it was the database) the following strtotime (nothing to do with our database) has the same behaviour:

date("l dS M Y H:i", strtotime('2004-08-12 10:18'));

The above two date()'s combined with strtotime() just show it as it's inserted in the database; not modified to the user's timezone

We also had another problem (which was an expected one) that the dates we inserted into our database were being inserted at the user's timezone. We expected that, so instead we're now using gmdate() for the date of insertion, which is perfect.

Any help you can provide will be gratefully appreciated

    $date = date_create('2004-08-12 10:18');
    
    $tz = timezone_open('Africa/Gaborone');
    date_timezone_set($date, $tz);
    echo $date->format('M-d-Y H:i:s e') . "</br>\n"; 
    
    $tz = timezone_open('Australia/Melbourne');
    date_timezone_set($date, $tz);
    echo $date->format('M-d-Y H:i:s e') . "</br>\n"; 

    outputs:

    Aug-12-2004 16:18:00 Africa/Gaborone
    Aug-13-2004 00:18:00 Australia/Melbourne

    The above I believe requires PHP > 5.2. If thats not possible, I'd assume you'd have to calculate the ofset between the two dates, and adjust your output accordingly.

    Hope this helps.

      Hi thanks for your reply 🙂

      I looked up that function, date_timezone_set() and it seems it needs a date, then the the timezone

      This isn't going to work for us. Basically, I was hoping that just by using that original date_default_timezone_set($timezone); from my original code, it would just alter all the dates. It's done that, except for anything with strtotime. We have so many it wouldn't be viable to edit all our dates (there's thousands of them) for use with date_timezone_set

      Any further help on fixing / explaining this would be greatly appreciated. I'm still confused though why it works on everything except the strtotime's 😕

      Appreciate your help ferritt

        Seems to me the above really is what your looking for... heres another example, this time using the date_default_timezone_set function instead:

        date_default_timezone_set('Australia/Melbourne');
        
        $date1 = date_create('2004-08-12 10:18');
        $date2 = date_create('2004-08-12 10:18'); 
        $tz = timezone_open('UTC'); 
        date_timezone_set($date2, $tz); 
        echo $date1->format('M-d-Y H:i:s e') . "</br>\n"; 
        echo $date2->format('M-d-Y H:i:s e') . "</br>\n";

        date1 and date2 are identical, but the two echo statements return different values. date1 shows the the time in the default timezone, and date2 shows it transformed into antother timezone. so, i guess what i'm really illustrating isn't so much the date_timezone_set function, but using the DateTime object as opposed to a string value.

        IOW, rather that using the date() function, use the date_create() function instead, and refrence is using the $date->format() method shown above. translation based on the timezone set based on the date_default_timezone_set() function.

        KWIM?

          Hi ferritt, thanks for your continued support 🙂

          I see now how you've done that.

          However, for my implementation, it would mean I'm going to have to edit every date, right? This is virtually impossible to do for us (there's so many, it wouldn't be viable), so if so, I'll have to give this up as a bad job!

          Just to clarify (as I'm getting a little confused), why does it change normal date()'s but not ones that have a strtotime? My assumption was that with that date_default_timezone_set() set it would alter all dates; it does, just not the strtotimes

          I've seen some other sites that have done this, and also use a database, so I might check with them to see if they can let me know what they used.

          Thanks again, I appreciate your help 🙂

            Well, just a guess, but I'd say that functions such as date() are based on the unix timestamp, which doesn't account for timezones at all. So, regardless of the location of where the date() item is attempting to describe, it's always is in relation to the local server. the value of date isn't anything more than a... long integer, perhaps?

            When you think about it, what is the intrepreter to do to calcualte timezones? difference west of UTC? difference east of UTC? Maybe a little of both? How will it deal wierd timezones that are 30 minutes off, instead of a full hour?

            Well, good luck with your project... hope you find a way to work it out anyway.

              Write a Reply...