-1

How can I convert a $datetime in PHP (servertime) to Users local timezone using JS?

<span class="datetime"><?php echo $datetime; ?></span>

Is this possible?

McBabba
  • 39
  • 5

3 Answers3

0

You can use momentjs or moment timezone

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD HH:mm:ss ZZ';
    return moment(time, format).tz(zone).format(format);
}

For more :- Convert date to another timezone in JavaScript

Community
  • 1
  • 1
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
0

How about this. Set your initial value to be a unix timestamp:

<span class="datetime"><?php echo strtotime($datetime); ?></span>

Then use JQuery to replace that timestamp with the datetime in the local timezone:

$(function() {
  $(".datetime").each(function(idx) {
     var converted = new Date( parseInt($(this).text()) * 1000 );
     $(this).text( converted.toString("ddd MMM d yyyy H:mm:ss") );
  });
});
Cully
  • 6,427
  • 4
  • 36
  • 58
-1

Better send unix timestamp to the frontend and then convert that into a local date using Javascript.

Neo
  • 6,753
  • 1
  • 19
  • 25
  • What you're suggesting is exactly what the OP is asking in his/her question. – Cully May 21 '14 at 06:46
  • No, I am suggesting a way of doing it so that it is easy for backend and frontend to do the conversions. Backend does not need to know about the frontend timezone and can always send the unix timestamp which can be easily converted by JS. – Neo May 21 '14 at 06:49