class DatexObject in Datex 7
Same name and namespace in other branches
- 7.2 datex_api/datex_api.class.inc \DatexObject
This class is Jalali equivilant of php DateTime. It also has some functionallity from object defiend in Drupal's date module DateObject.
Hierarchy
- class \DatexObject
Expanded class hierarchy of DatexObject
1 string reference to 'DatexObject'
- DatexObjectUtils::toArray in datex_api/
datex_api_classes.inc - Returns granularity parts of a given date in an array.
File
- datex_api/
datex_api_classes.inc, line 621 - API and helper functions used by other datex modules.
View source
class DatexObject {
protected $dateobj;
public $error;
public $errorMessage;
public $hasError;
public $tz;
protected $formatString;
/**
* Constructor for DatexObject.
*
* @param mixed $datetime
* Given date/time
* @param bool $is_gregorian
* Indicates wheter given date to constructor is Gregorian or not, default
* is set by a constant in module file.
* @param DateTimezone $tz
* DateTimeZone to use.
* @param string $format
* format used for formatting date.
*/
public function __construct($datetime = NULL, $is_gregorian = DATEX_GREGORIAN, $tz = NULL, $format = NULL) {
$this->hasError = FALSE;
$this->error = '';
$this->errorMessage = '';
$this
->setDatetime($datetime, $is_gregorian, $tz);
$format = $format ? $format : 'Y-m-d';
$this
->setFormat($format);
$tz = DatexFormatter::getTzObject($tz);
if ($tz instanceof DateTimeZone) {
$this->dateobj
->setTimezone($tz);
}
return $this;
}
/**
* Magic Function toString.
*
* Returns date stored in this function, Formatted according to internal
* format string, As an string.
*/
public function __toString() {
return $this
->format();
}
/**
* Clones internal DateTime object when cloning instance of this class.
*/
public function __clone() {
$this->dateobj = clone $this->dateobj;
}
/**
* Reset Date/Time to now.
*/
public function reset() {
$this->dateobj = new DateTime(NULL, $this->dateobj
->getTimezone());
return $this;
}
/**
* Similar to DateTime::format().
*
* @param string $format
* Format string.
* @param bool $use_intl
* Whether to use php-intl or not.
*/
public function format($format = NULL, $use_intl = DATEX_USE_INTL) {
$format = $format ? $format : $this->formatString;
$tz = $this->dateobj
->getTimezone();
return DatexFormatter::format($this->dateobj, $format, $tz, $use_intl);
}
/**
* Returns a clone of internal DateTime object.
*
* Cloned object always contains Gregorian date converted from jalali date
* given to DatexObject.
*/
public function getDateobjClone() {
return clone $this->dateobj;
}
/**
* Set's date from given date.
*
* For accepted list of accepted date formats,
* See DatexFormatter::ObjectFromDate.
*/
public function setDatetime($datetime = NULL, $gregorian = DATEX_GREGORIAN, $tz = NULL) {
$tz = DatexFormatter::getTzObject($tz);
$this->dateobj = DatexFormatter::ObjectFromDate($datetime, $tz, $gregorian);
return $this;
}
/**
* Sets date and time zone.
*/
public function setDate($year = NULL, $month = NULL, $day = NULL, $tz = NULL) {
if (!$year) {
$year = $this
->format('Y');
}
if (!$mont) {
$month = $this
->format('n');
}
if (!$day) {
$day = $this
->format('j');
}
$date = DatexFormatter::toGregorian($year, $month, $day);
$this
->xsetDate($date['year'], $date['month'], $date['day']);
if ($tz = DatexFormatter::getTzObject($tz)) {
$this
->setTimezone($tz);
}
return $this;
}
/**
* Set format string used for formating date.
*/
public function setFormat($format) {
$this->formatString = $format;
return $this;
}
/**
* Returns format string set by setFormat.
*/
public function getFormat() {
return $this->formatString;
}
/**
* Same as DateTime::getTimezone().
*/
public function getTimezone() {
return $this->dateobj
->getTimezone();
}
/**
* Sets Time Zone of internal date object.
*
* Accepts a DateTimeZone Object or an string representing a timezone.
*/
public function setTimezone($timezone) {
$timezone = DatexFormatter::getTzObject($timezone);
$this->dateobj
->setTimezone($timezone);
return $this;
}
/**
* Same as DateTime::setTimestamp().
*/
public function setTimestamp($timestamp) {
$this->dateobj
->setTimestamp($timestamp);
return $this;
}
/**
* Same as DateTime::setTime().
*/
public function setTime($hour, $minute, $second = 0) {
$this->dateobj
->setTime($hour, $minute, $second);
return $this;
}
/**
* Same as DateTime::getOffset().
*/
public function getOffset() {
return $this->dateobj
->getOffset();
}
/**
* Same as DateTime::diff().
*/
public function xdiff(DateTime $datetime2, $absolute = FALSE) {
return $this->dateobj
->diff($datetime2, $absolute);
}
/**
* Same as DateTime::format().
*/
public function xformat($format) {
return $this->dateobj
->format($format);
}
/**
* Same as DateTime::getLastErrors().
*/
public function xgetLastErrors() {
return $this
->getLastErrors();
}
/**
* Same as DateTime::setDate().
*/
public function xsetDate($year, $month, $day) {
return $this->dateobj
->setDate($year, $month, $day);
}
/**
* Same as DateTime::getTimestamp().
*/
public function getTimestamp() {
return $this->dateobj
->format('U');
}
/**
* Same as DateTime::modify().
*/
public function modify($modify) {
$this->dateobj
->modify($modify);
return $this;
}
/**
* Returns an object containing first day of Jalali month.
*/
public function monthFirstDay() {
$date = clone $this;
$date
->setDate(NULL, NULL, 1);
return $date;
}
/**
* Returns an object containing last day of Jalali month.
*/
public function monthLastDay() {
$date = clone $this;
$date
->setDate(NULL, NULL, DatexFormatter::format($date, 't'));
return $date;
}
/**
* Returns date granularities put in an array.
*
* @return array
* Date granularities put in an array.
*/
public function toArray() {
return array(
'year' => $this
->format('Y'),
'month' => $this
->format('n'),
'day' => $this
->format('j'),
'hour' => $this
->format('H'),
'minute' => $this
->format('i'),
'second' => $this
->format('s'),
'timezone' => $this
->format('e'),
);
}
/**
* Returns amount of time difference to another date object.
*
* @throws Exception if given measure is week, it will throw an exception, it
* is not implemented yet.
*/
public function difference(DatexObject $date2_in, $measure = 'seconds', $absolute = TRUE) {
// Create cloned objects or original dates will be impacted by the
// date_modify() operations done in this code.
$date1 = $this
->getDateobjClone();
$date2 = $date2_in
->getDateobjClone();
$diff = date_format($date2, 'U') - date_format($date1, 'U');
if ($diff == 0) {
return 0;
}
elseif ($diff < 0 && $absolute) {
// Make sure $date1 is the smaller date.
$temp = $date2;
$date2 = $date1;
$date1 = $temp;
$diff = date_format($date2, 'U') - date_format($date1, 'U');
}
$year_diff = intval(date_format($date2, 'Y') - date_format($date1, 'Y'));
switch ($measure) {
case 'seconds':
return $diff;
case 'minutes':
return $diff / 60;
case 'hours':
return $diff / 3600;
case 'years':
return $year_diff;
case 'months':
$format = 'n';
$item1 = $this
->format_php($format, $date1);
$item2 = $this
->format_php($format, $date2);
if ($year_diff == 0) {
return intval($item2 - $item1);
}
else {
$item_diff = 12 - $item1;
$item_diff += intval(($year_diff - 1) * 12);
return $item_diff + $item2;
}
break;
case 'days':
$format = 'z';
$item1 = $this
->format_php($format, $date1);
$item2 = $this
->format_php($format, $date2);
if ($year_diff == 0) {
return intval($item2 - $item1);
}
else {
$item_diff = date_days_in_year($date1) - $item1;
for ($i = 1; $i < $year_diff; $i++) {
date_modify($date1, '+1 year');
$item_diff += date_days_in_year($date1);
}
return $item_diff + $item2;
}
break;
case 'weeks':
default:
break;
}
return NULL;
}
/**
* Same as DatexObject toArray but in Gregorian format.
*
* @return array
* An array of date granuls.
*/
public function xtoArray() {
return array(
'year' => $this->dateobj
->format('Y'),
'month' => $this->dateobj
->format('n'),
'day' => $this->dateobj
->format('j'),
'hour' => intval($this->dateobj
->format('H')),
'minute' => intval($this->dateobj
->format('i')),
'second' => intval($this->dateobj
->format('s')),
'timezone' => $this->dateobj
->format('e'),
);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DatexObject:: |
protected | property | ||
DatexObject:: |
public | property | ||
DatexObject:: |
public | property | ||
DatexObject:: |
protected | property | ||
DatexObject:: |
public | property | ||
DatexObject:: |
public | property | ||
DatexObject:: |
public | function | Returns amount of time difference to another date object. | |
DatexObject:: |
public | function | Similar to DateTime::format(). | |
DatexObject:: |
public | function | Returns a clone of internal DateTime object. | |
DatexObject:: |
public | function | Returns format string set by setFormat. | |
DatexObject:: |
public | function | Same as DateTime::getOffset(). | |
DatexObject:: |
public | function | Same as DateTime::getTimestamp(). | |
DatexObject:: |
public | function | Same as DateTime::getTimezone(). | |
DatexObject:: |
public | function | Same as DateTime::modify(). | |
DatexObject:: |
public | function | Returns an object containing first day of Jalali month. | |
DatexObject:: |
public | function | Returns an object containing last day of Jalali month. | |
DatexObject:: |
public | function | Reset Date/Time to now. | |
DatexObject:: |
public | function | Sets date and time zone. | |
DatexObject:: |
public | function | Set's date from given date. | |
DatexObject:: |
public | function | Set format string used for formating date. | |
DatexObject:: |
public | function | Same as DateTime::setTime(). | |
DatexObject:: |
public | function | Same as DateTime::setTimestamp(). | |
DatexObject:: |
public | function | Sets Time Zone of internal date object. | |
DatexObject:: |
public | function | Returns date granularities put in an array. | |
DatexObject:: |
public | function | Same as DateTime::diff(). | |
DatexObject:: |
public | function | Same as DateTime::format(). | |
DatexObject:: |
public | function | Same as DateTime::getLastErrors(). | |
DatexObject:: |
public | function | Same as DateTime::setDate(). | |
DatexObject:: |
public | function | Same as DatexObject toArray but in Gregorian format. | |
DatexObject:: |
public | function | Clones internal DateTime object when cloning instance of this class. | |
DatexObject:: |
public | function | Constructor for DatexObject. | |
DatexObject:: |
public | function | Magic Function toString. |