Jump to content

Date/Time based on user's location


demeritrious

Recommended Posts

I would really appreciate help and I hope my problem can be solved easily. I am attempting to display the date/time based on the user's location. I have installed the timezone.js plugin for help as well. Is there a way to convert the Jquery variable that displays the users timezone location and insert it inside the PHP date_default_timezone_set?

 

If there is an easier way to display the users timezone date and time please let me know otherwise this was the only solution I could find.

 

Thanks in advanced...

 

Here is my code

 

Header

<header>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="Time/detect_timezone.js"></script>
<script src="Time/jquery.detect_timezone.js"></script>
<script>
$(document).ready(function(){
  $('#tzvalue').set_timezone({'default' : 'America/Los_Angeles'});
});
</script>
</header>
 
Currently I have: (the timezone currently appears in the text box and below the textbox PHP echos the current time in my timezone)

<input type="text" Name="Timezone" id="tzvalue"></input>
 
<?php
date_default_timezone_set("America/Chicago");
echo "The time is " . date("h:i:sa");
?>

What I want is:


<?php
date_default_timezone_set("$tzvalue");
echo "The time is " . date("h:i:sa");
?>

 

Link to comment
Share on other sites

you canget  the client's timezone in JavaScript

<script>
    var curdate = new Date()
var offset = curdate.getTimezoneOffset()
    document.write(offset);
</script>

will get this

-180

 

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight savings time prevents this value from being a constant even for a given locale

 

so you can use this time difference to show the time in the user's time zone

more about this here

http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript

Link to comment
Share on other sites

You might want to Google jsTimezoneDetect that is a  jQuery plugin that determines Timezone based on the user's location. I would give the link here, but for some reason one of the moderator's said I was going against PHP Freaks policies when I gave a link the last time..  :confused:

Link to comment
Share on other sites

OOPS   :shrug:

 

Though I did create a script that grabs the timezone using PHP, Ajax and JQuery :

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>TimeZone</title>
  </head>

  <body>
    <p>Today's Date and Time is <span id="result"></span></p>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="jstz.min.js"></script>
    <script>
      $(document).ready(function() {
        var timezone = jstz.determine(),
            myTimezone = timezone.name(), // Grab my Timezone:
            params = {timezone: myTimezone}, // Create an object

        /* A serialized repesentation of an object, suitable for an Ajax Request */
        myData = jQuery.param(params);

        /* The Ajax Request */
        $.ajax({
          type: "post",
          url: "myDateScript.php",
          data: myData, // The data that is being sent to myDateScript.php
          success: function(info) {
            $('#result').html(info); // Display the result back when saved: 
          }
        }); // End of Ajax Request:				
      }); // End of Doc Ready:
    </script>
  </body>
</html>

The PHP

<?php
// Date & time format.
$date_format = "m/d/Y";
$time_format = "h:i A";

// Set timezone.
$timezone = $_POST['timezone']; // The Ajax Request $_POST['timezone']:
//$timezone = America/New_York';


// Get current datetime.
$date = new DateTime();

// Set timezone.
$date->setTimezone(new DateTimeZone($timezone));

// Echo current date and time... plus send the response back:
echo $date->format($date_format . " " . $time_format); 

Maybe the scripts can modify it to fit what he wants? 

Edited by Strider64
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.