You are here

public static function DatexFormatter::ObjectFromDate in Datex 7

Created a DateTime object containing date from $date.

Accepted date formats are an integer (as timestamp), A DatexObject object, A DateTime or DateObject object or an array.

If an array is given and $gregorian is set to FALSE, Then Object is extracted from a Jalali date. This is the only way of getting date from jalali date.

3 calls to DatexFormatter::ObjectFromDate()
DatexFormatter::formatPHP in datex_api/datex_api_classes.inc
Formats a date according to format given.
DatexFormatter::getTimestamp in datex_api/datex_api_classes.inc
Generates timestamp from given date.
DatexObject::setDatetime in datex_api/datex_api_classes.inc
Set's date from given date.

File

datex_api/datex_api_classes.inc, line 392
API and helper functions used by other datex modules.

Class

DatexFormatter
Date tools for Jalali Dates.

Code

public static function ObjectFromDate($date = NULL, $tz = NULL, $gregorian = TRUE) {

  // If is a number or a timestamp string: like 432432 or '432432', it
  // will become like '@432432'. This case covers both integer and string.
  if (is_numeric($date)) {
    $date = '@' . $date;
  }
  elseif (is_object($date)) {
    $date = '@' . $date
      ->format('U');
  }
  elseif (is_array($date)) {
    if (!$gregorian) {
      $greg_date = self::toGregorian(@$date['year'], @$date['month'], @$date['day']);
      $year = $greg_date['year'];
      $month = $greg_date['month'];
      $day = $greg_date['day'];
    }
    else {
      $year = isset($date['year']) ? intval($date['year']) : intval(date('Y'));
      $month = isset($date['month']) ? intval($date['month']) : intval(date('n'));
      $day = isset($date['day']) ? intval($date['day']) : intval(date('j'));
    }
    $hour = isset($date['hour']) ? intval($date['hour']) : intval(date('G'));
    $minute = isset($date['minute']) ? intval($date['minute']) : intval(date('i'));
    $second = isset($date['second']) ? intval($date['second']) : intval(date('s'));
    $date = '@' . mktime($hour, $minute, $second, $month, $day, $year);
  }
  elseif ($date == NULL) {
    $date = 'now';
  }

  // Why doesn't the freaking DateTime accept NULL az tz?!!
  return isset($tz) ? new DateTime($date, $tz) : new DateTime($date);
}