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\Utility\Argument; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use WBW\Library\Core\Utility\Argument\StringUtility; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Date utility. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Library\Core\Utility\Argument |
22
|
|
|
* @final |
23
|
|
|
*/ |
24
|
|
|
final class DateUtility implements DateUtilityInterface { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get a day number. |
28
|
|
|
* |
29
|
|
|
* @param DateTime $date The date. |
30
|
|
|
* @return integer Returns the day number between 1 and 7 with monday equals to 1. |
31
|
|
|
*/ |
32
|
|
|
public static function getDayNumber(DateTime $date) { |
33
|
|
|
return intval($date->format("N")); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get a month number. |
38
|
|
|
* |
39
|
|
|
* @param DateTime $date The date. |
40
|
|
|
* @return integer Returns the month number. |
41
|
|
|
*/ |
42
|
|
|
public static function getMonthNumber(DateTime $date) { |
43
|
|
|
return intval($date->format("m")); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get a week number. |
48
|
|
|
* |
49
|
|
|
* @param DateTime $date The date. |
50
|
|
|
* @return integer Returns the week number. |
51
|
|
|
*/ |
52
|
|
|
public static function getWeekNumber(DateTime $date) { |
53
|
|
|
return intval($date->format("W")); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get a week number to apply with a schedule. |
58
|
|
|
* |
59
|
|
|
* <p> |
60
|
|
|
* For example: |
61
|
|
|
* We have a schedule etablished over 5 weeks. |
62
|
|
|
* |
63
|
|
|
* We start the schedule with the week number 1. |
64
|
|
|
* If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 1 |
65
|
|
|
* If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 2 |
66
|
|
|
* etc. |
67
|
|
|
* |
68
|
|
|
* We start the schedule with the week number 3. |
69
|
|
|
* If the current date is 2018-01-01 and the start date is 2018-01-01, the week number is 3 |
70
|
|
|
* If the current date is 2018-01-08 and the start date is 2018-01-01, the week number is 4 |
71
|
|
|
* etc. |
72
|
|
|
* </p> |
73
|
|
|
* |
74
|
|
|
* @param DateTime $date The date. |
75
|
|
|
* @param DateTime $startDate The start date. |
76
|
|
|
* @param integer $weekCount The week count. |
77
|
|
|
* @param integer $weekNumber The week number. |
78
|
|
|
* @return integer Returns the week number to apply between 1 and $weekCount. |
79
|
|
|
*/ |
80
|
|
|
public static function getWeekNumberToApply(DateTime $date, DateTime $startDate, $weekCount, $weekNumber) { |
81
|
|
|
|
82
|
|
|
// Check the date arguments. |
83
|
|
|
if ($date < $startDate) { |
84
|
|
|
return -1; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Check the week arguments. |
88
|
|
|
if ($weekCount <= 0 || $weekNumber <= 0 || $weekCount < $weekNumber) { |
89
|
|
|
return -1; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Calculate. |
93
|
|
|
$result = intval($date->diff($startDate)->d / 7) + $weekNumber; |
94
|
|
|
if ($weekCount < $result) { |
95
|
|
|
$result -= $weekCount; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Return. |
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the weekday into FR. |
104
|
|
|
* |
105
|
|
|
* @return array Returns the weekday into FR. |
106
|
|
|
*/ |
107
|
|
|
public static function getWeekdayFR() { |
108
|
|
|
return [ |
109
|
|
|
self::EN_WEEKDAY_SUNDAY => self::FR_WEEKDAY_SUNDAY, |
110
|
|
|
self::EN_WEEKDAY_MONDAY => self::FR_WEEKDAY_MONDAY, |
111
|
|
|
self::EN_WEEKDAY_TUESDAY => self::FR_WEEKDAY_TUESDAY, |
112
|
|
|
self::EN_WEEKDAY_WEDNESDAY => self::FR_WEEKDAY_WEDNESDAY, |
113
|
|
|
self::EN_WEEKDAY_THURSDAY => self::FR_WEEKDAY_THURSDAY, |
114
|
|
|
self::EN_WEEKDAY_FRIDAY => self::FR_WEEKDAY_FRIDAY, |
115
|
|
|
self::EN_WEEKDAY_SATURDAY => self::FR_WEEKDAY_SATURDAY, |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get a year number. |
121
|
|
|
* |
122
|
|
|
* @param DateTime $date The date. |
123
|
|
|
* @return integer Returns the year number. |
124
|
|
|
*/ |
125
|
|
|
public static function getYearNumber(DateTime $date) { |
126
|
|
|
return intval($date->format("Y")); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Translate the weekday part. |
131
|
|
|
* |
132
|
|
|
* @param string $date The date. |
133
|
|
|
* @param array $translations The translations: day => translation. |
134
|
|
|
* @return string Returns the weekday part translated. |
135
|
|
|
*/ |
136
|
|
|
public static function translateWeekday($date, array $translations = []) { |
137
|
|
|
return StringUtility::replace($date, array_keys($translations), array_values($translations)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|