Db object / AttrTimestamp: add support of millisecond / microsecond
This commit is contained in:
parent
630e2c4d02
commit
df4cc74fea
2 changed files with 16 additions and 8 deletions
12
src/Date.php
12
src/Date.php
|
@ -86,12 +86,20 @@ class Date {
|
||||||
* @return \DateTime
|
* @return \DateTime
|
||||||
*/
|
*/
|
||||||
public static function from_timestamp($value, $timezone=null) {
|
public static function from_timestamp($value, $timezone=null) {
|
||||||
$date = new \DateTime();
|
$date = \DateTime::createFromFormat("U.u", number_format($value, 6, ".", ""));
|
||||||
$date -> setTimestamp($value);
|
|
||||||
$date -> setTimezone(self :: timezone($timezone));
|
$date -> setTimezone(self :: timezone($timezone));
|
||||||
return $date;
|
return $date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert \DateTime object to timestamp as float with microsecond
|
||||||
|
* @param \DateTime $value The \DateTime object to convert
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public static function to_timestamp($value) {
|
||||||
|
return floatval($value->format("U.u"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create \DateTime object
|
* Create \DateTime object
|
||||||
* @param int|null $timezone The expected timezone (optional, default: system one)
|
* @param int|null $timezone The expected timezone (optional, default: system one)
|
||||||
|
|
|
@ -18,11 +18,11 @@ class AttrTimestamp extends Attr {
|
||||||
* The export format
|
* The export format
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $export_format = 'Y/m/d H:i:s';
|
protected $export_format = 'Y/m/d H:i:s.u';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute attribute value from DB
|
* Compute attribute value from DB
|
||||||
* @param int|null $value The value as retrieved from debug
|
* @param int|float|null $value The value as retrieved from debug
|
||||||
* @return \DateTime|null The attribute value
|
* @return \DateTime|null The attribute value
|
||||||
*/
|
*/
|
||||||
public function from_db($value) {
|
public function from_db($value) {
|
||||||
|
@ -33,14 +33,14 @@ class AttrTimestamp extends Attr {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute attribute value for DB
|
* Compute attribute value for DB
|
||||||
* @param \DateTime|int|null $value The value as handled in PHP
|
* @param \DateTime|int|float|null $value The value as handled in PHP
|
||||||
* @return int|null The attribute value as stored in DB
|
* @return float|null The attribute value as stored in DB
|
||||||
*/
|
*/
|
||||||
public function to_db($value) {
|
public function to_db($value) {
|
||||||
$value = parent::from_db($value);
|
$value = parent::from_db($value);
|
||||||
if (is_null($value)) return null;
|
if (is_null($value)) return null;
|
||||||
$value = $value instanceof \DateTime?$value:Date :: from_timestamp($value);
|
$value = $value instanceof \DateTime?$value:Date :: from_timestamp($value);
|
||||||
return $value->getTimestamp();
|
return Date :: to_timestamp($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,7 +61,7 @@ class AttrTimestamp extends Attr {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute attribute value to string
|
* Compute attribute value to string
|
||||||
* @param \DateTime|int|null $value The input value as handled in PHP
|
* @param \DateTime|int|float|null $value The input value as handled in PHP
|
||||||
* @return string The attribute value as string
|
* @return string The attribute value as string
|
||||||
*/
|
*/
|
||||||
public function to_string($value) {
|
public function to_string($value) {
|
||||||
|
|
Loading…
Reference in a new issue