javascript - Need to use the user's local time in a PHP script to send an email - how can I take the data from JS and use it in my php? -


this question has answer here:

need use user's local time in php script send email - how can take data js , use in php?

ideally me time need:

var = new date(); var hrs = now.gethours(); var msg = "";  if (hrs >  0) msg = "mornin' sunshine!"; // if (hrs >  6) msg = "good morning";      // after 6am if (hrs > 12) msg = "good afternoon";    // after 12pm if (hrs > 17) msg = "good evening";      // after 5pm if (hrs > 22) msg = "go bed!";        // after 10pm  alert(msg); 

but need know how can use data js variable in php script. happens after user submits contact form, collect user's time in visit form , pass php before need use it. ajax solution here? i'm learning ajax first time, hear, has sort of functionality.

if want date send when form submitted, don't need ajax. have hidden input field in form when form submitted, uses script find time , sets fields value (which posted through php).

it strikes me looking @ example though should use php's built in date function, can change timezone etc. if expect timezone change clients, might best pass at least timezone through javascript whole date.

other options, on first visit site run similar script we've discussed here - grab user's date/time javascript , possibly use ajax send php script save timezone offset session can use whenever want. e.g.

<?php  // example script... if( !isset($_session['timezone_offset'])) : ?> <script> // javascript here 'client_datetime' etc  // ajax here post 'client_datetime' yourscript.php </script> <?php endif; ?>  -- yourscript.php <?php  if( isset($_post['client_datetime']) ) {  $server_time = strtotime( date('y-m-d h:ia') ); $client_time = strtotime( $_post['client_datetime'] );  $offset = $server_time - $client_time;  $_session['timezone_offset'] = $offset;  }  ?>  -- whenever want use date or time in php:  <?php  // clients date , time echo date('y-m-d h:ia', strtotime(date('y-m-d h:ia')) + $_session['timezone_offset']);  ?> 

lots of options... easiest i'd use javascript attach form onsubmit() becomes post variable use in php.

if want use ajax send php, make sure set session when php receives , don't request again - no point in sending multiple requests server retrieve same data!


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -