|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi; |
|
15
|
|
|
|
|
16
|
|
|
use DateTime as PHPDateTime; |
|
17
|
|
|
use DateTimeZone; |
|
18
|
|
|
use WebHemi\I18n\TimeZoneInterface; |
|
19
|
|
|
use WebHemi\StringLib; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class DateTime. |
|
23
|
|
|
* |
|
24
|
|
|
* Under php5-fpm the default DateTime object will be instantiated as TimeCopDateTime which makes issues in the |
|
25
|
|
|
* dependency injection adapter, and also fails unit tests. So the idea is to create this empty extension and |
|
26
|
|
|
* use this class instead of the default one or the TimeCopDateTime. |
|
27
|
|
|
*/ |
|
28
|
|
|
class DateTime extends PHPDateTime implements TimeZoneInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
private $timeZoneDataPath; |
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
private $dateFormat = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* DateTime constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $time |
|
39
|
|
|
* @param DateTimeZone|null $timeZone |
|
40
|
|
|
*/ |
|
41
|
32 |
|
public function __construct($time = 'now', DateTimeZone $timeZone = null) |
|
42
|
|
|
{ |
|
43
|
32 |
|
if (is_numeric($time)) { |
|
44
|
2 |
|
$time = date('Y-m-d H:i:s', $time); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
32 |
|
if (empty($timeZone)) { |
|
48
|
30 |
|
$currentTimeZone = new DateTimeZone(date_default_timezone_get()); |
|
49
|
|
|
} else { |
|
50
|
3 |
|
$currentTimeZone = $timeZone; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
32 |
|
$this->timeZoneDataPath = __DIR__.'/I18n/TimeZone'; |
|
54
|
32 |
|
$this->loadTimeZoneData($currentTimeZone->getName()); |
|
55
|
|
|
|
|
56
|
32 |
|
parent::__construct($time, $currentTimeZone); |
|
57
|
32 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Loads date format data regarding to the time zone. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $timeZone |
|
63
|
|
|
*/ |
|
64
|
32 |
|
public function loadTimeZoneData(string $timeZone) : void |
|
65
|
|
|
{ |
|
66
|
32 |
|
$normalizedTimeZone = StringLib::convertNonAlphanumericToUnderscore($timeZone, '-'); |
|
67
|
|
|
|
|
68
|
32 |
|
if (file_exists($this->timeZoneDataPath.'/'.$normalizedTimeZone.'.php')) { |
|
69
|
31 |
|
$this->dateFormat = include $this->timeZoneDataPath.'/'.$normalizedTimeZone.'.php'; |
|
70
|
|
|
} |
|
71
|
32 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns date formatted according to given format. |
|
75
|
|
|
* @param string $format |
|
76
|
|
|
* @return string |
|
77
|
|
|
* @link http://php.net/manual/en/datetime.format.php |
|
78
|
|
|
*/ |
|
79
|
21 |
|
public function format($format) |
|
80
|
|
|
{ |
|
81
|
21 |
|
if (isset($this->dateFormat[$format])) { |
|
82
|
1 |
|
$timestamp = $this->getTimestamp(); |
|
83
|
1 |
|
$extraFromat = $this->dateFormat[$format]; |
|
84
|
|
|
|
|
85
|
1 |
|
if (strpos($extraFromat, '%Q') !== false) { |
|
86
|
1 |
|
if ($this->dateFormat['ordinals']) { |
|
87
|
1 |
|
$replace = date('jS', $timestamp); |
|
88
|
|
|
} else { |
|
89
|
|
|
$replace = '%d.'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$extraFromat = str_replace('%Q', $replace, $extraFromat); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
$dateString = strftime($extraFromat, $timestamp); |
|
96
|
|
|
} else { |
|
97
|
21 |
|
$dateString = parent::format($format); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
21 |
|
return $dateString; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Checks if stored timestamp belong to current day. |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
4 |
|
public function isToday() : bool |
|
109
|
|
|
{ |
|
110
|
4 |
|
return date('Ymd', $this->getTimestamp()) == date('Ymd'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Checks if stored timestamp belong to current month. |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
4 |
|
public function isCurrentMonth() : bool |
|
119
|
|
|
{ |
|
120
|
4 |
|
return date('Ym', $this->getTimestamp()) == date('Ym'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Checks if stored timestamp belong to current year. |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
4 |
|
public function isCurrentYear() : bool |
|
129
|
|
|
{ |
|
130
|
4 |
|
return date('Y', $this->getTimestamp()) == date('Y'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns the date with the default format. |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function __toString() : string |
|
139
|
|
|
{ |
|
140
|
1 |
|
return $this->format('Y-m-d H:i:s'); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|