-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDateTimeFactory.php
More file actions
72 lines (57 loc) · 1.77 KB
/
Copy pathDateTimeFactory.php
File metadata and controls
72 lines (57 loc) · 1.77 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
<?php
/*
* This file is part of the core-library package.
*
* (c) 2023 WEBEWEB
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace WBW\Library\Common\Factory;
use DateTime;
use InvalidArgumentException;
use Throwable;
use WBW\Library\Common\Helper\DateTimeHelper;
use WBW\Library\Common\Helper\DateTimeMethod;
/**
* Date/time factory.
*
* @author webeweb <https://github.com/webeweb>
* @package WBW\Library\Common\Factory
*/
class DateTimeFactory {
/**
* First and last date of month.
*
* @param DateTime $date The date.
* @return DateTime[] Returns the dates.
*/
public static function firstLastDateMonth(DateTime $date): array {
return [
(clone $date)->modify("first day of this month"),
(clone $date)->modify("last day of this month"),
];
}
/**
* Range from a date/time to another date/time.
*
* @param DateTime $from From date/time.
* @param DateTime $to To date/time.
* @return DateTime[]|null Returns the date/time range.
* @throws InvalidArgumentException Throws an illegal argument exception if the two date/time does not have the same time zone.
* @throws Throwable Throws an exception if an error occurs.
*/
public static function range(DateTime $from, DateTime $to): ?array {
if (false === DateTimeHelper::isLessThan($from, $to)) {
return null;
}
$range = [];
$current = clone $from;
while (false === DateTimeHelper::isGreaterThan($current, $to)) {
$range[] = clone $current;
DateTimeMethod::addDay($current, 1);
}
return $range;
}
}