|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2018 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Helper\Argument; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Date/time helper. |
|
18
|
|
|
* |
|
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
20
|
|
|
* @package WBW\Library\Core\Helper\Argument |
|
21
|
|
|
*/ |
|
22
|
|
|
class DateTimeHelper { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Compare two date/time. |
|
26
|
|
|
* |
|
27
|
|
|
* @param DateTime $a The date/time. |
|
28
|
|
|
* @param DateTime $b The date/time. |
|
29
|
|
|
* @return int Returns |
|
30
|
|
|
* -1: if the date/time A is greater than date/time B |
|
31
|
|
|
* 1: if the date/time A is lesser than date/time B |
|
32
|
|
|
* 0: if the date/time are equals. |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function compare(DateTime $a, DateTime $b) { |
|
35
|
|
|
$timestampA = $a->getTimestamp(); |
|
36
|
|
|
$timestampB = $b->getTimestamp(); |
|
37
|
|
|
if ($timestampA < $timestampB) { |
|
38
|
|
|
return -1; |
|
39
|
|
|
} |
|
40
|
|
|
if ($timestampB < $timestampA) { |
|
41
|
|
|
return 1; |
|
42
|
|
|
} |
|
43
|
|
|
return 0; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Determines if two date/time are equals. |
|
48
|
|
|
* |
|
49
|
|
|
* @param DateTime $a The date/time. |
|
50
|
|
|
* @param DateTime $b The date/time. |
|
51
|
|
|
* @return boolean Returns true in case o success, false otherwise. |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function equals(DateTime $a, DateTime $b) { |
|
54
|
|
|
return 0 === self::compare($a, $b); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get a day number. |
|
59
|
|
|
* |
|
60
|
|
|
* @param DateTime $dateTime The date/time. |
|
61
|
|
|
* @return integer Returns the day number between 1 and 7 with monday equals to 1. |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function getDayNumber(DateTime $dateTime) { |
|
64
|
|
|
return intval($dateTime->format("N")); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get a month number. |
|
69
|
|
|
* |
|
70
|
|
|
* @param DateTime $dateTime The date/time. |
|
71
|
|
|
* @return integer Returns the month number. |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getMonthNumber(DateTime $dateTime) { |
|
74
|
|
|
return intval($dateTime->format("m")); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get a week number. |
|
79
|
|
|
* |
|
80
|
|
|
* @param DateTime $dateTime The date/time. |
|
81
|
|
|
* @return integer Returns the week number. |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function getWeekNumber(DateTime $dateTime) { |
|
84
|
|
|
return intval($dateTime->format("W")); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get a year number. |
|
89
|
|
|
* |
|
90
|
|
|
* @param DateTime $dateTime The date/time. |
|
91
|
|
|
* @return integer Returns the year number. |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function getYearNumber(DateTime $dateTime) { |
|
94
|
|
|
return intval($dateTime->format("Y")); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|