Here is my code:
<script>
var g = moment.tz.guess();
</script>
<?php
$detectTimezone = new DateTimeZone('America/Los_Angeles');
?>
how can I replace America/Los_Angeles
with var g?
Here is my code:
<script>
var g = moment.tz.guess();
</script>
<?php
$detectTimezone = new DateTimeZone('America/Los_Angeles');
?>
how can I replace America/Los_Angeles
with var g?
Simple answer is You can't.
<?php
$detectTimezone = new DateTimeZone('America/Los_Angeles');
?>
Above code executes on server side, and JavaScript runs on client side.
The only way to do is using AJAX requests.
I agree with all the comments saying that normally you cannot, just for the sake of completeness, there are workarounds indeed, like the accepted answer in here: Access a JavaScript variable from PHP.
I would not recommended it however for various reasons, most importantly because it makes things unclear and unnecessary complicated. As others said, it would help to state first what you are aiming to achieve?