1

I am developing a module, where:

  1. The administrator enters time in (0-23) format.These values gets saved in database.
  2. In the front end (for the website users) I need to fetch the current client time (not the server time), maybe using JavaScript, and then comparing it with the stored time (in PHP variables with current time fetched using JavaScript).

I want to do something like the following.

// $time_one: from the database.
// $time_two: from the database.
// $currentTime:  client current time.

if (currentTime > $time_one && currentTime < $time_two) {
  // Display some message.
}

How can this be done? Can PHP and JavaScript be mixed in the module file?

2 Answers 2

0

You can pass values from PHP to JavaScript using code similar to the following one.

drupal_add_js(
  array('myModule' => array('time_one' => $time_one, 'time_two' => $time_two)),
  'setting'
);

$time_one and $time_two are the PHP variables that contain the values you fetch from the database.

JavaScript would access those values using the following code.

time_one = Drupal.settings.myModule.time_one;
time_two = Drupal.settings.myModule.time_two;
2
  • Thanks!!, this works for me.. BTW is there a way to send javascript variable to php. I mean if i can just send the current time stored in a javascript variable to php? I tried this with cookies but i think its not the best way to do it.
    – yashbinani
    Commented May 24, 2012 at 17:55
  • You can do that with jQuery, its $.ajax(), and a custom menu callback as URL of the request. See the data parameter $.ajax() accepts.
    – avpaderno
    Commented May 24, 2012 at 19:43
0

Send server time to the client side via js variables: drupal_add_js, see example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.