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?
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?
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
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") );
});
});
Better send unix timestamp to the frontend and then convert that into a local date using Javascript.