Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Last active December 11, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/4566991 to your computer and use it in GitHub Desktop.
Save mindplay-dk/4566991 to your computer and use it in GitHub Desktop.
A CBehavior (for the Yii-framework) that automatically converts to/from DATE/DATETIME values
<?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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment