-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate.php
68 lines (53 loc) · 1.97 KB
/
Date.php
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
<?php
declare(strict_types=1);
namespace Heptacom\HeptaConnect\Dataset\Base;
final class Date extends \DateTime
{
public function __construct(string $time = 'now', ?\DateTimeZone $timezone = null)
{
parent::__construct($time, $timezone);
$this->setTime(0, 0);
}
public static function createFromDateTime(\DateTimeInterface $dateTime): Date
{
return new Date('@' . $dateTime->getTimestamp(), $dateTime->getTimezone());
}
public static function createDateFromFormat(string $format, string $time, ?\DateTimeZone $timezone = null): ?Date
{
$dateTime = parent::createFromFormat($format, $time, $timezone);
return $dateTime === false ? null : static::createFromDateTime($dateTime);
}
public static function createDateFromImmutable(\DateTimeImmutable $datetTimeImmutable): Date
{
return static::createFromDateTime(parent::createFromImmutable($datetTimeImmutable));
}
public function asDateTime(): \DateTimeInterface
{
return new \DateTime('@' . $this->getTimestamp(), $this->getTimezone());
}
public function add(\DateInterval $interval): Date
{
return static::createFromDateTime(parent::add($this->removeTimeFromInterval($interval)));
}
public function sub(\DateInterval $interval): Date
{
return static::createFromDateTime(parent::sub($this->removeTimeFromInterval($interval)));
}
public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): Date
{
return parent::setTime(0, 0, 0, 0);
}
public function setTimestamp(int $unixtimestamp): Date
{
$time = $unixtimestamp % (24 * 60 * 60);
return parent::setTimestamp($unixtimestamp - $time);
}
private function removeTimeFromInterval(\DateInterval $interval): \DateInterval
{
$interval = clone $interval;
$interval->h = 0;
$interval->m = 0;
$interval->s = 0;
return $interval;
}
}