TL;DR | My Problem: I am just trying to get the "Time Zone" for the date_default_timezone_set();
to set the local time zone within the PHP script. I am setting a variable $abc
with a Javascript to find out the user's time zone. While, on doing echo of the variable $abc, I am getting the required output ("Asia/Kolkata")., when I use it directly within date_default_timezone_set($abc);
it simply output's the java script/the content within the variable.
So, this is a pretty basic problem. I have got my timestamps in GMT and I want them to be changed accordingly with the user's timezone.
I have sourced this java script from the web which works pretty fine in getting the time zone:
<script>
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
document.write(tz)
</script>
The output in my case was:
Asia/Calcutta
Great right? Nope. Here comes the problem. So as I try to convert it into a normal PHP variable and then pass it on to date_default_timezone_set(); I get the error. Have a look at the PHP code below.
<?php
// In GMT
$yourDate = "2019-09-12 00:00:00";
$abc = "<script>
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
document.write(tz)
</script>";
$timestamp = strtotime($yourDate);
date_default_timezone_set($abc);
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
echo $date->format('d/m/Y H:i:s') . "\n";
echo "<br>";
// It does echos Asia/Kolkata
echo $abc;
?>
So the echo $abc
does outputs "Asia/Kolkata" but date_default_timezone_set($abc);
echos the content of $abc
and does not runs the JavaScript before outputting the text.
Well, I couldn't find a working and easy to implement solution yet, could someone please shed some light on this? If not this, what will be the best way to convert GMT to local time as of today?
I am looking for a solution to convert UTC to Local Time Zone here the best possible way. I am looking for a way to get the inputs for this PHP line date_default_timezone_set($abc);