1
|
|
|
<?php |
2
|
|
|
namespace common\components; |
3
|
|
|
|
4
|
|
|
use yii; |
5
|
|
|
use \DateTime; |
6
|
|
|
use \DateTimeZone; |
7
|
|
|
|
8
|
|
|
class Time extends \yii\base\BaseObject implements \common\interfaces\TimeInterface { |
9
|
|
|
|
10
|
|
|
public const EARLIEST_DATE = '2014-01-01'; |
11
|
|
|
|
12
|
|
|
public $timezone; |
13
|
|
|
|
14
|
|
|
public function __construct(String $timezone, $config = []) { |
15
|
|
|
$this->timezone = $timezone; |
16
|
|
|
parent::__construct($config); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/* |
20
|
|
|
* Parses the supplied string into a `\DateTime` object of the |
21
|
|
|
* given `$format`. It assumes the supplied string is in the |
22
|
|
|
* timezone specified in $this->timezone. |
23
|
|
|
* |
24
|
|
|
* @param string $time the questionable time to parse |
25
|
|
|
* @param string $format the format `$time` is expected to be in |
26
|
|
|
* @return \DateTime the parsed time or false |
27
|
|
|
*/ |
28
|
|
|
public function parse(string $time, string $format = 'Y-m-d') { |
29
|
|
|
$dt = DateTime::createFromFormat($format, $time, new DateTimeZone($this->timezone)); |
30
|
|
|
if($dt) { |
31
|
|
|
// for some reason, using createFromFromat adds in the time. The regular DateTime constructor _does not_ do this. We manually zero out the time here to make the DateTime objects match. |
32
|
|
|
$dt->setTime(0, 0, 0); |
33
|
|
|
$formatted = $dt->format($format); |
34
|
|
|
if($formatted === $time && $this->inBounds($dt)) { |
35
|
|
|
return $dt; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
return false; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/* |
42
|
|
|
* Checks if the given `\DateTime` is within acceptable date bounds. |
43
|
|
|
* It does no good to have the date be far in the past or in the future. |
44
|
|
|
* |
45
|
|
|
* @param \DateTime $dt |
46
|
|
|
* @return boolean |
47
|
|
|
*/ |
48
|
|
|
public function inBounds(DateTime $dt) { |
49
|
|
|
$first = strtotime((new DateTime(self::EARLIEST_DATE))->format('Y-m-d')); |
50
|
|
|
$test = strtotime($dt->format('Y-m-d')); |
51
|
|
|
$now = strtotime($this->getLocalDate()); |
52
|
|
|
|
53
|
|
|
if($first <= $test && $test <= $now) { |
54
|
|
|
return true; |
55
|
|
|
} else { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function convertLocalToUTC($local, $inc_time = true) { |
61
|
|
|
$fmt = $inc_time ? "Y-m-d H:i:s" : "Y-m-d"; |
62
|
|
|
|
63
|
|
|
$timestamp = new DateTime($local, new DateTimeZone($this->timezone)); |
64
|
|
|
$timestamp->setTimeZone(new DateTimeZone("UTC")); |
65
|
|
|
return $timestamp->format($fmt); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function convertUTCToLocal($utc, $iso = true) { |
69
|
|
|
$fmt = $iso ? Datetime::ATOM : "Y-m-d H:i:s"; |
70
|
|
|
|
71
|
|
|
$timestamp = new DateTime($utc, new DateTimeZone("UTC")); |
72
|
|
|
$timestamp->setTimeZone(new DateTimeZone($this->timezone)); |
73
|
|
|
return $timestamp->format($fmt); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getLocalTime($timezone = null) { |
77
|
|
|
if($timezone === null) |
78
|
|
|
$timezone = $this->timezone; |
79
|
|
|
|
80
|
|
|
$timestamp = new DateTime("now", new DateTimeZone($timezone)); |
81
|
|
|
return $timestamp->format("Y-m-d H:i:s"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getLocalDate($timezone = null) { |
85
|
|
|
if($timezone === null) |
86
|
|
|
$timezone = $this->timezone; |
87
|
|
|
|
88
|
|
|
return (new DateTime("now", new DateTimeZone($timezone))) |
89
|
|
|
->format("Y-m-d"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function alterLocalDate($date, $modifier) { |
93
|
|
|
return (new DateTime("$date $modifier", new DateTimeZone($this->timezone))) |
94
|
|
|
->format("Y-m-d"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getUTCBookends($local) { |
98
|
|
|
$local = trim($local); |
99
|
|
|
if(strpos($local, " ")) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$start = $local . " 00:00:00"; |
104
|
|
|
$end = $local . "23:59:59"; |
105
|
|
|
|
106
|
|
|
$front = self::convertLocalToUTC($start); |
|
|
|
|
107
|
|
|
$back = self::convertLocalToUTC($end); |
108
|
|
|
|
109
|
|
|
return [$front, $back]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|