Automatically Detect User's Current Local Time with JavaScript or PHP

Automatically detect user's current local time with JavaScript or PHP

In short no.

I would suggest using server side time, and have the ability for the user to choose their time zone.

You could possibly calculate default time zones for users based on heuristics around their IP address, but this is open to error.

You should be able to extract client side time zone information through javascript / Ajax, but as with the time itself this is also open to error.

My recommendation: Let users choose their time zone with a sensible default based on where you expect your users to be.

How to automatically detect user's timezone?

Use javascript solution

http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/

Demo should show your timezone in select box.

http://onlineaspect.com/examples/timezone/index.html (Dead link)

How can I get the user's local time instead of the server's time?

As mentioned by everyone PHP only displays server side time.

For client side, you would need Javascript, something like the following should do the trick.

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

if (minutes < 10) {
minutes = "0" + minutes;
}

document.write("<b>" + hours + ":" + minutes + " " + "</b>");

And if you want the AM/PM suffix, something like the following should work:

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();

var suffix = "AM";

if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}

if (hours == 0) {
hours = 12;
}

if (minutes < 10) {
minutes = "0" + minutes;
}

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");

Here is a list of additional JavaScript Date and Time functions you could mess around with.

How to detect user's timezone?

To summarize Matt Johnson's answer in terms of code:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
var tz = jstz.determine(); // Determines the time zone of the browser client
var timezone = tz.name(); //For e.g.:"Asia/Kolkata" for the Indian Time.
$.post("url-to-function-that-handles-time-zone", {tz: timezone}, function(data) {
//Preocess the timezone in the controller function and get
//the confirmation value here. On success, refresh the page.
});
});
</script>

Get user timezone string with Javascript / PHP?

You can't do this in PHP alone.

You can use Javascript to set the value in a cookie, then use PHP to read the cookie on the next page (re)load.

Javascript:

var dateVar = new Date()
var offset = dateVar.getTimezoneOffset();
document.cookie = "offset="+offset;

PHP:

echo $_COOKIE['offset'];

Use this to convert the offset to the friendly timezone name in PHP. Javascript returns the offset in minutes, while this PHP function expects the input to be in seconds - so multiply by 60. The third parameter is a boolean value of whether or not you are in Daylight Savings Time. Read the manual and update the code to fit your needs.

echo timezone_name_from_abbr("", intval($_COOKIE['offset'])*60, 0);

http://php.net/manual/en/function.timezone-name-from-abbr.php

Get local time in PHP

try this method

Here are various method choose your method and try

<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>


Related Topics



Leave a reply



Submit