9

I'm pulling the raw generated mysql timestamp info of $item_date from the database as php date format:

if (($timestamp = strtotime($item_date)) === false) { echo "The timestamp string is bogus"; } else { echo date('j M Y h:i:sA', $timestamp); } 

Output folowwing the server zone (UTC):

12 Nov 2012 05:54:11PM

but i want it to convert according to the user time zone

Example: let's say if the user's time is 13 Nov 2012 07:00:00 AM(+0800 GMT) and the server time is 12 Nov 2012 11:00:00 PM(UTC) and the timestamp of $item_date is 12 Nov 2012 10:30:00 PM (UTC) so

User with (UTC) will see $item_date as:

12 Nov 2012 10:30:00 PM

and user with (+0800 GMT) will see $item_date as:

13 Nov 2012 06:30:00 PM

How do i get it done? Thanks

4
  • If you know user timezone, then you can use date_default_timezone_set('America/Los_Angeles'); Commented Nov 12, 2012 at 6:12
  • but that is only setting it to one zone i want it to be able to detect the user's zone of all type like +6gmt -8gmt etc all of it... Commented Nov 12, 2012 at 6:15
  • have you stored each user timezone in DB ? Commented Nov 12, 2012 at 6:21
  • No the database timestap is all in UTC i want it to be like this: $item_date timestamp > PHP Date Format follow the user's computer time zone > output final PHP date... Commented Nov 12, 2012 at 6:25

1 Answer 1

27

This post has been updated to include a full-fledged example

<?php session_start(); if (isset($_POST['timezone'])) { $_SESSION['tz'] = $_POST['timezone']; exit; } if (isset($_SESSION['tz'])) { //at this point, you have the users timezone in your session $item_date = 1371278212; $dt = new DateTime(); $dt->setTimestamp($item_date); //just for the fun: what would it be in UTC? $dt->setTimezone(new DateTimeZone("UTC")); $would_be = $dt->format('Y-m-d H:i:sP'); $dt->setTimezone(new DateTimeZone($_SESSION['tz'])); $is = $dt->format('Y-m-d H:i:sP'); echo "Timestamp " . $item_date . " is date " . $is . " in users timezone " . $dt->getTimezone()->getName() . " and would be " . $would_be . " in UTC<br />"; } ?> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js"></script> <script language="javascript"> $(document).ready(function() { <?php if (!isset($_SESSION['tz'])) { ?> $.ajax({ type: "POST", url: "tz.php", data: 'timezone=' + jstz.determine().name(), success: function(data){ location.reload(); } }); <?php } ?> }); </script> 

I hope this is now clear enough ;).

Sign up to request clarification or add additional context in comments.

9 Comments

Sorry i'm confused, where do i put the $item_date timestamp at?
instead of time() in the setTimestamp() function
Hey there, I have updated the post to include a complete example that should make you happy now ;). Please mark the post as accepted, if this answers your question entirely.
Hey there, we are getting closer ;). Use DateTime::createFromFormat to initialize from your mysql date. Do it like this: $dt = DateTime::createFromFormat("Y-m-d H:i:s", $item_date); and remove the call $dt->setTimestamp($item_date);. Be sure to save the page as tz.php, as the ajax call is invoking that very page.
Hey Thanks for helping me until this far, it's now working! My host is using PHP 5.2 so DateTime::createFromFormat does not work but i used this to work around it: $item_date = '2012-11-12 23:15:20'; with $dt = new DateTime($item_date);
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.