Show time in local timezone with PHP & Javascript easily

Hi Guys,

We always come into a situation where we need to handle time in users local timezone. This can be achieved in different ways

One is to ask users their current timezone on the time of registration and store into database along with other user data. its better to store the value of timezone in name format like (Asia/Kolkata or America/New_York), which could be used directly mentioning to PHP function to set current timezone

<?php
date_default_timezone_set("Asia/Kolkata");
or
date_default_timezone_set("America/New_York");
?>

Other way I used is PHP DateTime and DateTimeZone classes along with momentjs library. Below are the urls of that momentjs library files which I used in my code (you can also get the latest version file from same cdn domain)

https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js
https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.min.js

So what we are going to start with is getting users current timezone name and store it into cookie. For more easy I am going to write a JavaScript function for set cookie here too.

<script type="text/javascript">
window.onload = function(){
    timezone_offset_minutes = moment.tz.guess();
    setCookie('timezone_offset_minutes',timezone_offset_minutes,30);
}
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
</script>

So here I am using momentjs to capture user’s timezone name and storing it into the cookie. Next you can read that cookie value and assign to a variable in PHP via following code

<?php
if(isset($_COOKIE['timezone_offset_minutes']) && strlen(trim($_COOKIE['timezone_offset_minutes'])) > 0) {
    $timezone_name = $_COOKIE['timezone_offset_minutes'];
}
$currentTimeZone = (isset($timezone_name)) ? $timezone_name : 'UTC';
?>

Further we are going to get current time on the basis of UTC and next set the user timezone like following code

<?php
$currentTime = new DateTime('now', new DateTimeZone('UTC'));
$currentTime->setTimeZone(new DateTimeZone($currentTimeZone));
?>

Further we can have a date from database or user given date which we are going to show into users local timezone, even we stored that date into UTC timezone. So first line on the below code is setting date value in UTC timezone and store into a variable $startTime. In next line this variable $startTime is used to set timezone for user local timezone.

$startTime = new DateTime(date('m/d/Y H:i:s',strtotime($checkPasswordData['start_time'])), new DateTimeZone('UTC'));
$startTime->setTimeZone(new DateTimeZone($currentTimeZone));

Now your $startTime variable will work for user’s local timezone and return date in users local timezone.

To get users local timezone date timestamp use following function. You can use this function to compare date as timestamp

<?php
echo $startTime->getTimestamp();
?>

To get users local timezone date in any format, use following function

<?php
echo $startTime->format('M d, Y H:i:s');
?>

See Here: Other Blogs list

I hope this will be helpful for all or feel free to let me know in comments if you need more details from me on this blog post.

Thanks

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

two + seven =