0

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?

ideally this would get me the time I need:

var now = new Date();
var hrs = now.getHours();
var msg = "";

if (hrs >  0) msg = "Mornin' Sunshine!"; // REALLY early
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 to bed!";        // After 10pm

alert(msg);

But I need to know how I can use the data from a JS variable in the PHP script. This happens after the user submits a contact form, so I could collect the user's time in the visit to the form and pass that to php before I actually need to use it. Would AJAX be my solution here? I'm just learning AJAX for the first time, but from what I hear, it has this sort of functionality.

  • 1
    Try a javascript method that runs on page load and posts the users date/time back to a PHP backend script that can generate and send the email. – Chris Rasco Oct 07 '13 at 20:56

1 Answers1

1

If you want the date to be send when the form is submitted, you don't need Ajax. You could do something like have a hidden input field in your form which when the form is submitted, it uses your script to find the time and sets the fields value to that (which will then be posted through to PHP).

It strikes me looking at your example though that you should just use PHP's built in date function, you can change the timezone etc. If you expect the timezone will change often for your clients, might be best to pass at least the timezone through Javascript but probably the whole date.

Other options, on the first visit to your site you could run a similar script as we've discussed here - grab the user's date/time with Javascript and possibly even use Ajax to send it to a PHP script which will save the timezone offset to a session so you can use it whenever you want. E.g.

<?php

// example script...
if( !isset($_SESSION['timezone_offset'])) : ?>
<script>
// javascript here to get 'client_datetime' etc

// ajax here to post the 'client_datetime' to 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 you want to use date or time in PHP:

<?php

// clients date and time
echo date('Y-m-d H:ia', strtotime(date('Y-m-d H:ia')) + $_SESSION['timezone_offset']);

?>

Lots of options... Easiest I'd say would be to use Javascript to attach it to your form onSubmit() so it becomes a post variable to use in your PHP.

If you do want to use Ajax to send it to PHP, make sure you set a session when PHP receives it and don't request again - no point in sending multiple requests to the server to retrieve the same data!

scrowler
  • 24,273
  • 9
  • 60
  • 92