You are here

function webform_strtodate in Webform 7.4

Same name and namespace in other branches
  1. 6.3 webform.module \webform_strtodate()
  2. 7.3 webform.module \webform_strtodate()

Return a date in the desired format taking into consideration user timezones.

5 calls to webform_strtodate()
webform_conditional_prepare_date_js in includes/webform.conditionals.inc
Prepare a conditional value for adding as a JavaScript setting.
webform_conditional_prepare_time_js in includes/webform.conditionals.inc
Prepare a conditional value for adding as a JavaScript setting.
webform_expand_date in components/date.inc
Form API #process function for Webform date fields.
webform_expand_time in components/time.inc
Form API #process function for Webform time fields.
_webform_edit_date_validate in components/date.inc
Implements hook_form_id_validate().

File

./webform.module, line 5242
This module provides a simple way to create forms and questionnaires.

Code

function webform_strtodate($format, $string, $timezone_name = NULL, $reference_timestamp = NULL) {
  global $user;

  // Adjust the time based on the user or site timezone.
  if (variable_get('configurable_timezones', 1) && $timezone_name == 'user' && $user->uid) {
    $timezone_name = isset($GLOBALS['user']->timezone) ? $GLOBALS['user']->timezone : 'UTC';
  }

  // If the timezone is still empty or not set, use the site timezone.
  if (empty($timezone_name) || $timezone_name == 'user') {
    $timezone_name = variable_get('date_default_timezone', 'UTC');
  }
  if (!empty($timezone_name) && class_exists('DateTimeZone')) {

    // Suppress errors if encountered during string conversion. Exceptions are
    // only supported for DateTime in PHP 5.3 and higher.
    try {
      @($timezone = new DateTimeZone($timezone_name));
      if (isset($reference_timestamp)) {

        // A reference for relative dates has been provided.
        // 1. Convert the reference timestamp (in UTC) to a DateTime.
        // 2. Set to time zone to the user or system timezone, recreating the
        //    reference time in the appropriate time zone.
        // 3. Set the time to midnight because when a non-referenced relative
        //    date is created without a time, it is created at midnight (0:00).
        // 4. Adjust to the specified relative (or absolute) time.
        @($datetime = new DateTime('@' . $reference_timestamp));
        @$datetime
          ->setTimezone($timezone)
          ->setTime(0, 0, 0)
          ->modify($string);
      }
      else {
        @($datetime = new DateTime($string, $timezone));
      }
      return @$datetime
        ->format($format);
    } catch (Exception $e) {
      return '';
    }
  }
  else {
    return date($format, isset($reference_timestamp) ? strtotime($string, $reference_timestamp) : strtotime($string));
  }
}