1

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);

Souvik
  • 131
  • 1
  • 8
  • 4
    You cannot evaluate javascript code on the server side. – alanfcm Sep 12 '19 at 22:26
  • @computercarguy I am not trying to get timestamps using Javscript here. I am just using it to get the time zone, it's not a duplicate question. – Souvik Sep 13 '19 at 03:42
  • @alanfcm while echoing the PHP variable, it does outputs the required "local timezone". I just want that dynamic value of local time zone each time in order to insert it within `date_default_timezone_set($abc);` - How can I possibly do that? – Souvik Sep 13 '19 at 03:51
  • Also, can you let me know, if there are any better ways of achieving the same goal using just PHP and Javascript? – Souvik Sep 13 '19 at 03:52
  • @Souvik This line `date_default_timezone_set($abc);` will make PHP try to parse this `date_default_timezone_set("");` Do you think this is possible ? – Accountant م Sep 13 '19 at 04:16
  • 2
    *"// It does echos Asia/Kolkata"* .... No it does echo your script, and when the browser executes this script it will write `Asia/Kolkata` don't think that the server(PHP) echoed that, open your page source(raw HTML) and you will see what PHP has echoed. – Accountant م Sep 13 '19 at 04:18
  • You need to use Ajax to pass to the server the client's time zone. – alanfcm Sep 13 '19 at 14:38
  • @Accountantم I didn't saw it that way. Thanks for the clarifications, the browser is executing the script. And yes definitely `date_default_timezone_set("");` is not possible. I agree with you. Do you have a possible solution to achieve the goal? – Souvik Sep 13 '19 at 17:28
  • @alanfcm can you suggest any references? – Souvik Sep 13 '19 at 17:41
  • @Souvik Yes I can show you a solution, do you use jQuery ? – Accountant م Sep 13 '19 at 21:51
  • @Accountantم yes I use jQuery. Reference will be fine. I just want the time zone from user's browser to feed into this line `date_default_timezone_set();` and I will take care of the rest. – Souvik Sep 14 '19 at 06:21

0 Answers0