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?


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

  array('myModule' => array('time_one' => $time_one, 'time_two' => $time_two)),

$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;
  • 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 May 24 '12 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. – kiamlaluno May 24 '12 at 19:43

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, privacy policy and cookie policy

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