You are here

function _scheduler_strtotime in Scheduler 7

Same name and namespace in other branches
  1. 5 scheduler.module \_scheduler_strtotime()
  2. 6 scheduler.module \_scheduler_strtotime()

Converts a time string from the user's timezone into a Unix timestamp.

This expects the time string to be in the date format configured by the user.

Parameters

string $str: A string containing a time in the user configured date format.

Return value

int The time in Unix timestamp representation (UTC). NULL if the given time string is empty (NULL, FALSE, an empty string or only containing whitespace). FALSE if the given time string is malformed.

4 calls to _scheduler_strtotime()
scheduler_node_presave in ./scheduler.module
Implements hook_node_presave().
scheduler_node_validate in ./scheduler.module
Implements hook_node_validate().
scheduler_tokens in ./scheduler.tokens.inc
Implements hook_tokens().
_scheduler_form_alter in ./scheduler.edit.inc
Helper function that does all the work for the real hook_form_alter().

File

./scheduler.module, line 272
Scheduler publishes and unpublishes nodes on dates specified by the user.

Code

function _scheduler_strtotime($str) {
  if ($str && trim($str) != "") {
    $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
    $date_only_format = variable_get('scheduler_date_only_format', SCHEDULER_DATE_ONLY_FORMAT);
    if (_scheduler_use_date_popup()) {

      // Date Popup currently returns the value into the $node using its default
      // format DATE_FORMAT_DATETIME but reduced to the same granularity as the
      // requested format. Until date issue http://drupal.org/node/1855810 is
      // resolved we can use the date_popup functions date_format_order() and
      // date_limit_format() to derive the format of the returned string value.
      $granularity = date_format_order($date_format);
      $date_format = date_limit_format(DATE_FORMAT_DATETIME, $granularity);
      $granularity = array(
        'day',
        'month',
        'year',
      );
      $date_only_format = date_limit_format(DATE_FORMAT_DATETIME, $granularity);
    }
    $str = trim(preg_replace('/\\s+/', ' ', $str));
    $time = _scheduler_strptime($str, $date_format);

    // If the time failed using the full $date_format or no time entry is
    // allowed, but date-only with a default time is enabled, check if the input
    // matches the "date only" date format.
    $time_only_format = variable_get('scheduler_time_only_format', SCHEDULER_TIME_ONLY_FORMAT);
    if ((!$time || $time_only_format == '') && variable_get('scheduler_allow_date_only', FALSE)) {
      if ($time = _scheduler_strptime($str, $date_only_format)) {

        // A time has been calculated, but also check that there was only a
        // date entered and no extra mal-formed time elements.
        if ($str == date($date_only_format, $time)) {

          // Parse the default time string into elements, then add the total
          // offset in seconds to the timestamp.
          $default_time = date_parse(variable_get('scheduler_default_time', SCHEDULER_DEFAULT_TIME));
          $time_offset = $default_time['hour'] * 3600 + $default_time['minute'] * 60 + $default_time['second'];
          $time += $time_offset;
        }
        else {

          // The date was not the only text entered, so reject it.
          $time = FALSE;
        }
      }
    }
  }
  else {

    // $str is empty.
    $time = NULL;
  }
  return $time;
}