|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) Andreas Heigl<[email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the MIT License. See LICENSE.md file in the project root |
|
7
|
|
|
* for full license information. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace DateTime; |
|
11
|
|
|
|
|
12
|
|
|
use Closure; |
|
13
|
|
|
use DateTime\Timezone\Timezone; |
|
14
|
|
|
use DateTimeImmutable; |
|
15
|
|
|
use DateTimeInterface; |
|
16
|
|
|
|
|
17
|
|
|
class DateTime |
|
18
|
|
|
{ |
|
19
|
|
|
private $datetime; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(string $datetimeString = 'now', Timezone $timezone = null) |
|
22
|
|
|
{ |
|
23
|
|
|
if (null === $timezone) { |
|
24
|
|
|
$timezone = Timezone::fromString('UTC'); |
|
25
|
|
|
} |
|
26
|
|
|
$timezoneThief = function (Timezone $timezone) { |
|
27
|
|
|
return $timezone->timezone; |
|
|
|
|
|
|
28
|
|
|
}; |
|
29
|
|
|
$timezoneThief = Closure::bind($timezoneThief, null, $timezone); |
|
30
|
|
|
|
|
31
|
|
|
$timezone = $timezoneThief($timezone); |
|
32
|
|
|
$this->datetime = new DateTimeImmutable($datetimeString, $timezone); |
|
33
|
|
|
$this->datetime->setTimezone($timezone); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function add(DateTimeInterval $interval) : self |
|
37
|
|
|
{ |
|
38
|
|
|
$self = clone($this); |
|
39
|
|
|
$self->datetime = $self->datetime->add($interval->getDateTimeInterval()); |
|
40
|
|
|
|
|
41
|
|
|
return $self; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function modify(string $modification) : self |
|
45
|
|
|
{ |
|
46
|
|
|
$self = clone($this); |
|
47
|
|
|
$self->datetime = $this->datetime->modify($modification); |
|
48
|
|
|
|
|
49
|
|
|
return $self; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function sub(DateTimeInterval $interval) : self |
|
53
|
|
|
{ |
|
54
|
|
|
$self = clone($this); |
|
55
|
|
|
$self->datetime = $this->datetime->sub($interval->getDateTimeInterval()); |
|
56
|
|
|
|
|
57
|
|
|
return $self; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function diff(DateTime $date) : DateTimeInterval |
|
61
|
|
|
{ |
|
62
|
|
|
return DateTimeInterval::fromDateTimeInterval( |
|
63
|
|
|
$this->datetime->diff($date->datetime) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function format(string $format) : string |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->datetime->format($format); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getTimestamp() : int |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->datetime->getTimestamp(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getTimezone() : Timezone |
|
78
|
|
|
{ |
|
79
|
|
|
return Timezone::fromString($this->datetime->getTimezone()->getName()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function createFromFormat( |
|
83
|
|
|
string $format, |
|
84
|
|
|
string $time, |
|
85
|
|
|
TimeZone $timezone = null |
|
86
|
|
|
) : self { |
|
87
|
|
|
if (null === $timezone) { |
|
88
|
|
|
$timezone = Timezone::fromString('UTC'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$timezoneThief = function (Timezone $timezone) { |
|
92
|
|
|
return $timezone->timezone; |
|
|
|
|
|
|
93
|
|
|
}; |
|
94
|
|
|
$timezoneThief = Closure::bind($timezoneThief, null, $timezone); |
|
95
|
|
|
$timezone = $timezoneThief($timezone); |
|
96
|
|
|
|
|
97
|
|
|
$datetime = DateTimeImmutable::createFromFormat($format, $time, $timezone); |
|
98
|
|
|
|
|
99
|
|
|
return self::createFromPhpDateTime($datetime); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public static function createFromPhpDateTime(DateTimeInterface $datetime) : self |
|
103
|
|
|
{ |
|
104
|
|
|
return new self( |
|
105
|
|
|
$datetime->format('c'), |
|
106
|
|
|
Timezone::fromString($datetime->getTimezone()->getName()) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|