0

javascript:

<script src="<?= URL::site('/public/js/jstz-1.0.4.min.js'); ?>" type="text/javascript"></script>
<script>
    $(document).ready(function() {  
    var tz = jstz.determine(); // Determines the time zone of the browser client
    });
</script>

php:

<?php 
 $timezone = '<script>
    var tz = jstz.determine(); 
    document.write(tz.name());
</script>';
 ?>
<?php
echo $timezone;
$tz = new DateTimeZone($timezone);//error producing line
$message_date = new DateTime($message_date); 
$message_date->setTimeZone($tz);
$messagedate = $message_date->format('Y-m-d H:i:s');
?>

Above is the javascript to get the timezone.I am getting the timezone via script and assigned that to a php variable.If i echo the php variable (echo $timezone) i am getting the correct timezone.But if i pass the same variable in timezone conversion code(please see error producing line),i am getting this error "Exception [ 0 ]: DateTimeZone::__construct(): Unknown or bad timezone (<script> var tz = jstz.determine(); document.write(tz.name()); </script>)".

Actually,it is taking the whole $timezone vatiable as string and not variable.Need solution to solve this.

Thanks

user2681579
  • 1,413
  • 2
  • 23
  • 50

1 Answers1

0

You can't really solve this the way you're trying to.

The source file is going to get loaded by the web server, handed to PHP, which will process all the PHP code (note that it's not processing any javascript code). Then the result gets sent to the client's browser, where any javascript code will be executed.

So when you run your code, the echo works because the javascript code is being output directly to the page and then intepreted by the client web browser to write the time zone to the page. The rest of the PHP code doesn't work because $timezone is just a string representation of an HTML script tag and some javascript. None of the HTML or javascript is processed or executed by PHP on the server side. The string full of HTML and javascript isn't a valid input for the DateTimeZone constructor, hence your error.

Put simply, the two types of code can't really interact directly. You can use a technology like AJAX to send information from javascript code back to the server, but that will be on a separate request and is a very different solution than what you're attempting here.

cajunc2
  • 206
  • 1
  • 3
  • Is it possible to make an ajax call on page load that is on page load via ajax call a php function to save this time zone in db. – user2681579 Feb 11 '14 at 17:46
  • An introduction/tutorial on AJAX is outsite the scope of this question (and likely this site in general). [Here](http://www.w3schools.com/ajax/ajax_intro.asp) is a good reference from W3C on how AJAX works and how to get started with the technique. – cajunc2 Feb 11 '14 at 17:47
  • Yes, it would be possible to get the time zone with javascript and send that back to the server to put in the database. This question might be helpful also: http://stackoverflow.com/questions/1905397/how-to-get-clients-timezone – cajunc2 Feb 11 '14 at 17:49