0

I'm trying to use jsTimezoneDetect to create a variable for my php code below.

 <?php
    $latest_mysqldate = new DateTime( $row["latest_entry"]);
    $latest_mysqldate->setTimezone(new DateTimeZone('America/New_York'));
    $latest_mysqldate = $latest_mysqldate->format('M-d-Y h:i a');
 ?> 

When I use the invoke command from the website http://pellepim.bitbucket.org/jstz/ it's not working. I'm using this code on a completely different webpage trying to figure out the java script first then after I figure that out want to try and put the result in a php variable.

 var timezone = jstz.determine();
 timezone.name(); 

I tried this code and it works so I know the java script is fine just don't understand how to take that and put that in my php code. Also if you could let me know why the invoke code by itself doesn't work I'd greatly appreciate it.

 <blah id="timezoneid">
 <script type="text/javascript">
     var timezone = document.getElementById('blahid').innerHTML = jstz.determine().name() + "<br/>";
 </script>

As always thanks to the community for your help!

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
ZeroAccess
  • 171
  • 2
  • 3
  • 14
  • To get the data from js into php, you should use AJAX. AJAX is easiest (certainly waaaay less typing) via jQuery. Is that approach okay with you, or do you need pure js? – cssyphus Jan 20 '14 at 21:44
  • I've never used AJAX, I'll do some Google searches for AJAX as I've heard about it a couple other times and it seems to be pretty powerful. I'm already using jquery for a few other things so not sure if jquery and AJAX counter each other but I'll do some research. – ZeroAccess Jan 20 '14 at 21:51
  • jQuery and AJAX work great together. Bonus: if use jQuery with AJAX, all the cross-browser compat work is done for you. `>` [These might help](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843) `<` Re storing the information on the server side, it will be user-dependent so you could use sessions (the `$_SESSION` supervar) or you could use PHP cookies -- or you could store the value locally (in a var, or using `localstorage` or js/jQuery `cookies`) and pass the value to your PHP scripts when needed. – cssyphus Jan 20 '14 at 21:55
  • There is no need for AJAX since all that is required is to send data to the server, which can be done in say the src attribute of an image or script element that has no effect on the visible page. – RobG Jan 20 '14 at 23:59
  • RobG can you elaborate on this or give me some terms I can Google to get more information? I originally thought I could just have the javascript write the results in the php function and would be very easy. I'm still reading up on AJAX which looks like it will help me do a few other things I want to do just like to know my options. – ZeroAccess Jan 21 '14 at 16:43
  • possible duplicate of [PHP timestamp date to user timezone](http://stackoverflow.com/questions/13339086/php-timestamp-date-to-user-timezone) – Matt Johnson-Pint Feb 21 '15 at 01:18

0 Answers0