failed_hard
failed_hard

Reputation: 157

PHP Timezone adjust as per user

Im creating a UI where users get to choose their timezone. My backend is PHP and I know you can do it a few ways but I had questions about all of them and which way is the best:

https://www.php.net/date_default_timezone_set

//where America/Los_Angeles will be changed based on user
date_default_timezone_set('America/Los_Angeles');

Does this change the server timezone for ever user or just what is being exported? is there any performance issues with this?

What if it was a function like this...?

date_default_timezone_set('America/Los_Angeles'); -08:00 set in php.ini

$date = "2014-01-01 12:00:00";
$new_time_zone = "America/New_York" // -05:00 = +3 hours difference

function adjust_time_zone($string, $zone){

   $datetime = new DateTime($string);
   $new_zone = new DateTimeZone($zone);
   $datetime->setTimezone($new_zone);

   return ($datetime->format('Y-m-d H:i:s'));
}
adjust_time_zone($date, $new_time_zone);

Results(i tested this):

2014-01-01 15:00:00 //3 hours ahead. 

in this format everything stamped in the system would be on LA time and exported would be the change...

I know there is allot of these Timezone threads but there seems to be allot of "i do it like this" and not any solid "this way blows my socks off="

Thanks for any help you can give!

Upvotes: 3

Views: 3831

Answers (3)

Michael
Michael

Reputation: 19

I found a great solution to the problem - and to adapt the date and time settings on my page according to the users settings.

Ready build js code Some developers that had the exact same problem build a javascript solution to the code. You don't need to know javascript as long as you follow these instructions.

  1. Go to https://bitbucket.org/pellepim/jstimezonedetect/src/default/
  2. Read through the readme file if you want to
  3. Either download the script as an NPM package or download it manually by pressing "downloads" in the left menu.
  4. If you pressed downloads to manually download the script press "Download repository" to get the files.
  5. Create a js folder - named "js" in your project. (optional)
  6. Unzip the jstimezonedetect files into the js folder. I chose to name the unziped file "jstimezonedetect".
  7. To reach the files above my current directory is /js/jstimezonedetect/.
  8. In your header code (important it is in header since the time-zone will affect the full code on the rest of your page unless you run a function.
  9. Enter the following code to your php file:

        echo '  
    
            <script src="js/jstimezonedetect/dist/jstz.min.js"></script>
    
            <script>
                document.cookie = "usertimezone="+jstz.determine().name();
            </script>
        ';
        $settimezone = $_COOKIE['usertimezone'];
        date_default_timezone_set($settimezone);
    

The first line of script will import the jstz.min.js file from the importat jstimezonedetect project.

Second line will create a cookie (you can later delete the cookie if you want to).

In the third line I get the cookie into the php script and then I use PHP date_default_timezone_set(); (https://www.php.net/manual/en/function.date-default-timezone-set.php) to set the timezone.

Worked perfectly for me. :) Ask if something is unclear.

Upvotes: 2

Quixrick
Quixrick

Reputation: 3200

Well it looks like what you have should work for you. This is an alternate method, although not necessarily better. You can just use PHP's mktime and pass the hour offset to it.

<?php
print "<br>Date: ".date("Y-m-d H:i:s");

// DEFINE AN OFFSET
$offset = -5; // TIMEZONE OFFSET: '-5' FOR NEW YORK

// ADD THE OFFSET TO THE CURRENT TIME
print "<br>Date Offset: ".$date_offset = date("Y-m-d H:i:s", mktime(date("H") + $offset, date("i"), date("s"), date("m"), date("d"), date("Y")));

Upvotes: 3

ssaltman
ssaltman

Reputation: 3713

I store everything in the db as a datetime and UTC timezone.

I store the user's timezone in the db ($timezone).

So when a time ($time) is taken from the db, I run it through the following function:

 function changetimefromUTC($time, $timezone) {
    $changetime = new DateTime($time, new DateTimeZone('UTC'));
    $changetime->setTimezone(new DateTimeZone($timezone));
    return $changetime->format('m/d/y h:i a');
 }

This returns the time in the user's timezone, formatted.

edit: you can see the db table of timezones in this thread:

Generating a drop down list of timezones with PHP

Upvotes: 3

Related Questions