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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With