demeritrious Posted September 14, 2014 Share Posted September 14, 2014 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/291054-datetime-based-on-users-location/ Share on other sites More sharing options...
abdul202 Posted September 21, 2014 Share Posted September 21, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291054-datetime-based-on-users-location/#findComment-1491749 Share on other sites More sharing options...
Strider64 Posted September 21, 2014 Share Posted September 21, 2014 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.. Quote Link to comment https://forums.phpfreaks.com/topic/291054-datetime-based-on-users-location/#findComment-1491762 Share on other sites More sharing options...
mac_gyver Posted September 21, 2014 Share Posted September 21, 2014 @Strider64, if you meant the pm from me, you were told not to put links to your site in replies, especially since the OP is already using the jquery timezone plugin and has a specific question about getting the time zone value to use in the php date_default_timezone_set() statement. Quote Link to comment https://forums.phpfreaks.com/topic/291054-datetime-based-on-users-location/#findComment-1491765 Share on other sites More sharing options...
Strider64 Posted September 21, 2014 Share Posted September 21, 2014 (edited) Well, in that case : http://pellepim.bitbucket.org/jstz/ Edited September 21, 2014 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/291054-datetime-based-on-users-location/#findComment-1491767 Share on other sites More sharing options...
mac_gyver Posted September 21, 2014 Share Posted September 21, 2014 He's already using that. Quote Link to comment https://forums.phpfreaks.com/topic/291054-datetime-based-on-users-location/#findComment-1491768 Share on other sites More sharing options...
Strider64 Posted September 21, 2014 Share Posted September 21, 2014 (edited) OOPS 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 September 22, 2014 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/291054-datetime-based-on-users-location/#findComment-1491770 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.