public
Last active

A CBehavior (for the Yii-framework) that automatically converts to/from DATE/DATETIME values

  • Download Gist
GDateTimeBehavior.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
<?php
 
/**
* This behavior implements synchronous timestamp-accessors for DATE and DATETIME values
* by convention - attributes with a "_date" or "_datetime" suffix, respectively, can be
* accessed as UNIX timestamps (integers) by leaving out the suffix; for example, a
* DATETIME attribute $test->created_datetime can be accessed as $test->created.
*
* This behavior can be attached to a CModel or descendants, e.g. CFormModel, CActiveRecord, etc.
*
* @method CModel getOwner()
*/
class GDateTimeAccessors extends CBehavior
{
/**
* @var string suffix for DATE attributes.
*/
public static $date_suffix = '_date';
 
/**
* @var string suffix for DATETIME attributes.
*/
public static $datetime_suffix = '_datetime';
 
/**
* @var string date format when updating DATE attributes; defaults to MySQL DATE format.
* @see date()
*/
public static $date_format = 'Y-m-d';
 
/**
* @var string date/time format when updating DATE attributes; defaults to MySQL DATETIME format.
* @see date()
*/
public static $datetime_format = 'Y-m-d H:i:s';
 
/**
* @param string $name
* @return bool
*/
public function canGetProperty($name)
{
$names = $this->getOwner()->attributeNames();
 
return in_array($name . self::$date_suffix, $names)
|| in_array($name . self::$datetime_suffix, $names);
}
 
/**
* @param string $name
* @return bool
*/
public function canSetProperty($name)
{
return $this->canGetProperty($name);
}
 
/**
* Dynamically convert a DATE or DATETIME attribute to a timestamp.
*
* @param string $name
* @return int|null
* @throws CException for invalid values in the DATE or DATETIME attribute
*/
public function __get($name)
{
/**
* @var CModel $owner
*/
 
$owner = $this->getOwner();
 
$names = $owner->attributeNames();
 
if (in_array($name . self::$date_suffix, $names)) {
$attr = $name . self::$date_suffix;
} else {
$attr = $name . self::$datetime_suffix;
}
 
$value = $owner->{ $attr };
 
if ($value) {
$time = strtotime($value);
 
if ($time === false) {
//throw new CException("property ".get_class($owner)."::\$$attr contains an invalid value: ".$value);
return null;
}
 
return $time;
}
 
return null;
}
 
/**
* Dynamically convert a timestamp to a DATE or DATETIME value.
*
* @param string $name
* @param int|null $value
*
* @throws CException for invalid values; only integer or null-values are accepted
* @return void
*/
public function __set($name, $value)
{
$owner = $this->getOwner();
 
if ($value !== null && ! is_int($value)) {
throw new CException("property ".get_class($owner)."::\$$name accepts only integer or null-values");
}
 
$names = $owner->attributeNames();
 
if (in_array($name . self::$date_suffix, $names)) {
$owner->{ $name . self::$date_suffix } = ($value === null)
? null
: date(self::$date_format, $value);
} else {
$owner->{ $name . self::$datetime_suffix } = ($value === null)
? null
: date(self::$datetime_format, $value);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.