3

I've asked a very similar question before but got no answers that helped.

I have a site that allows users to post notes. There will be a time stamp on those notes. The default timezone on my server is EST5EDT (despite me setting the date.timezone to something else, which is a different issue!).

As far as I can gather, it is best to set the timestamp with the server time and convert it for each user. For example:

User 1 (GMT) posts "Hello World" at (local time) 5:00 (server time) 0:00
User 2 (AEST, +10) sees that User 1 posted "Hello World" at (local time) 15:00

For the sake of argument, I am avoiding worrying about DST as I don't think it counts for this.

I understand I can use date_default_timezone_set() within my application but I am quite sure I should set the post time as the server time so no need to change the set timezone.

I only want to convert the time for the viewer

"Post as Server Time, Read as Local Time"

I do not believe I am the first person who has had their web app perform this so there must be an answer out there.

I will have to get the datetime the post was made, get the timezone of the user viewing the post (probably through javascript as php uses server side date and time) and convert the datetime using the user's timezone.

getTimezoneOffset() in javascript will work out the users time difference from UTC but not from my server time.

Wildcard27
  • 1,437
  • 18
  • 48

2 Answers2

1

What you can do is save the UTC time when saving and when viewing show the UTC time + offset. This can all be done using JS on client side.

JS method used when saving= UTC()

JS method used when displaying =

<html>
<head>
<script type="text/javascript">
        function showDateInClientSideFormat(dValue)
        {
            var d = new Date()
            var n = d.getTimezoneOffset();
            var dateClientSide = new Date(dValue +n);
            return dateClientSide;
        }
    </script>
</head>
<body>
<?php
echo '<script> showDateInClientSideFormat($dateSaved); </script>';
?>
</body>
</html>

PS: UTC time is the same as GMT time

DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
  • This is getting closer. The `var dateClientSide` will then have to be passed through php as I will be using a while loop to show all posts. Any ideas? – Wildcard27 Feb 05 '14 at 09:52
  • Better idea will be to encapsulate this piece of code in a JS function call like showDateInClientSideFormat($dateSaved) and call the function inside PHP loop. Note that you'll have to define the function before PHP code call – DhruvJoshi Feb 05 '14 at 09:56
  • This is really getting to be a pain in the butt, and a bit messy too! I guess you mean in my while loop use `echo "` wherever the comment datetime is shown? – Wildcard27 Feb 05 '14 at 10:08
  • @matt1985 Yes. Right! I've changed my answer to reflect your need. – DhruvJoshi Feb 05 '14 at 10:16
  • Sorry, @DhruvJoshi, I was busy reading up on timezone effects for my purpose. Although not able to test it, I am familiar with what your answer has provided and am sure it will be perfect. Thank you for your help. I cannot believe I only had 2 answers for this in over an hour! Been a pleasure working with you :-) – Wildcard27 Feb 05 '14 at 10:32
0

You could use PHPs DateTime and DateTimeZone to convert the datetime into the users current timezone:

http://www.php.net/manual/de/class.datetime.php
http://www.php.net/manual/de/class.datetimezone.php

$date = new DateTime($dateTimeFromDB);
$date->setTimeZone( new DateTimeZone('User/Timezone') );

echo $date->format('d.m.Y H:i');
gherkins
  • 14,603
  • 6
  • 44
  • 70