-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathARSerializableDateTime.php
More file actions
108 lines (90 loc) · 1.9 KB
/
ARSerializableDateTime.php
File metadata and controls
108 lines (90 loc) · 1.9 KB
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
<?php
/**
*
* @package activerecord
* @author Integry Systems
*/
class ARSerializableDateTime extends DateTime implements Serializable
{
protected $dateString;
private $isNull = false;
public function __construct($dateString = false)
{
if ($dateString instanceof ARValueMapper)
{
$dateString = $dateString->get();
}
$this->dateString = $dateString;
if(is_null($dateString) || '0000-00-00 00:00:00' == $dateString)
{
$this->isNull = true;
}
parent::__construct($dateString);
}
public static function createFromTimeStamp($timeStamp)
{
return new ARSerializableDateTime(date('Y-m-d H:i:s', $timeStamp));
}
public function isNull()
{
return $this->isNull;
}
public function format($format)
{
if($this->isNull())
{
return "";
}
else
{
return parent::format($format);
}
}
public function getTimeStamp()
{
return $this->format("U");
}
public function getTime()
{
return (int)$this->format("U");
}
/**
* Get a time difference in days from another DateTime object
*/
public function getDayDifference(DateTime $dateTime)
{
return $this->getSecDifference($dateTime) / 86400;
}
/**
* Get a time difference in seconds from another DateTime object
*/
public function getSecDifference(DateTime $dateTime)
{
return $this->format("U") - $dateTime->format("U");
}
public function serialize()
{
return serialize(array(
'dateString' => $this->dateString,
'isNull' => ($this->isNull() ? 'true' : 'false')
));
}
public function unserialize($serialized)
{
$dateArray = unserialize($serialized);
if($dateArray['isNull'] == 'true')
{
$dateString = null;
}
else
{
$dateString = $dateArray['dateString'];
}
self::__construct($dateString);
}
public function __toString()
{
return $this->format('Y-m-d H:i:s');
}
}
?>