Date::getMiddleWeek()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 31
Code Lines 17

Duplication

Lines 2
Ratio 6.45 %

Importance

Changes 0
Metric Value
dl 2
loc 31
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 16
nop 3
1
<?php
2
3
/**
4
 * Manage date
5
 *
6
 * @category  	lib
7
 * @author    	Judicaël Paquet <[email protected]>
8
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
9
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
10
 * @version   	Release: 1.0.0
11
 * @filesource	https://github.com/las93/venus2
12
 * @link      	https://github.com/las93
13
 * @since     	1.0
14
 */
15
namespace Venus\lib;
16
17
use \DateTime as DateTime;
18
19
/**
20
 * This class manage the date
21
 *
22
 * @category  	lib
23
 * @author    	Judicaël Paquet <[email protected]>
24
 * @copyright 	Copyright (c) 2013-2014 PAQUET Judicaël FR Inc. (https://github.com/las93)
25
 * @license   	https://github.com/las93/venus2/blob/master/LICENSE.md Tout droit réservé à PAQUET Judicaël
26
 * @version   	Release: 1.0.0
27
 * @filesource	https://github.com/las93/venus2
28
 * @link      	https://github.com/las93
29
 * @since     	1.0
30
 */
31
class Date
32
{
33
    /**
34
     * set name of image
35
     *
36
     * @access public
37
     * @param  int $iWeek number of week
38
     * @param  int $iYear year
39
     * @param string $sFormat
40
     * @return Date
41
     */
42
    public static function getWeek(int $iWeek, int $iYear, string $sFormat = "Y-m-d") : Date
43
    {
44
        $iFirstDayInYear = date("N",mktime(0, 0, 0, 1, 1, $iYear));
45
46 View Code Duplication
        if ($iFirstDayInYear < 5) { $iShift = -($iFirstDayInYear - 1) * 86400; } else { $iShift = (8 - $iFirstDayInYear) * 86400; }
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
48
        if ($iWeek > 1) { $iWeekInSeconds = ($iWeek-1) * 604800; } else { $iWeekInSeconds = 0; }
49
50
        $iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift;
51
        $iTimestampLastDay = mktime(0, 0, 0, 1, 6, $iYear) + $iWeekInSeconds + $iShift + 604800;
52
53
        return array(date($sFormat, $iTimestamp), date($sFormat, $iTimestampLastDay));
54
    }
55
56
    /**
57
     * set name of image
58
     *
59
     * @access public
60
     * @return \Venus\lib\Date
61
     */
62
    public static function getActualWeek() : Date
63
    {
64
        return self::getWeek(date('W'), date('Y'));
65
    }
66
67
    /**
68
     * set name of image
69
     *
70
     * @access public
71
     * @param  string $sMonth number of week
72
     * @param  string $sLanguage language
73
     * @return \Venus\lib\Date
74
     */
75
    public static function getMonthInWord(string $sMonth, string $sLanguage = 'fr') : Date
76
    {
77
        if ($sLanguage == 'fr') {
78
79
            if ($sMonth == '01' || $sMonth == 1) { return 'Janvier'; }
80
            else if ($sMonth == '02' || $sMonth == 2) { return 'Février'; }
81
            else if ($sMonth == '03' || $sMonth == 3) { return 'Mars'; }
82
            else if ($sMonth == '04' || $sMonth == 4) { return 'Avril'; }
83
            else if ($sMonth == '05' || $sMonth == 5) { return 'Mai'; }
84
            else if ($sMonth == '06' || $sMonth == 6) { return 'Juin'; }
85
            else if ($sMonth == '07' || $sMonth == 7) { return 'Juillet'; }
86
            else if ($sMonth == '08' || $sMonth == 8) { return 'Août'; }
87
            else if ($sMonth == '09' || $sMonth == 9) { return 'Septembre'; }
88
            else if ($sMonth == 10) { return 'Octobre'; }
89
            else if ($sMonth == 11) { return 'Novembre'; }
90
            else if ($sMonth == 12) { return 'Décembre'; }
91
        }
92
    }
93
94
    /**
95
     * set name of image
96
     *
97
     * @access public
98
     * @param  mixed $sDay number of day
99
     * @param  string $sLanguage language
100
     * @return \Venus\lib\Date
101
     */
102
    public static function getDayInWord(string $sDay, string $sLanguage = 'fr') : Date
103
    {
104
        if ($sLanguage == 'fr') {
105
106
            if ($sDay == 0) { return 'dimanche'; }
107
            else if ($sDay == 1) { return 'lundi'; }
108
            else if ($sDay == 2) { return 'mardi'; }
109
            else if ($sDay == 3) { return 'mercredi'; }
110
            else if ($sDay == 4) { return 'jeudi'; }
111
            else if ($sDay == 5) { return 'vendredi'; }
112
            else if ($sDay == 6) { return 'samedi'; }
113
        }
114
    }
115
116
    /**
117
     * get age by date
118
     *
119
     * @access public
120
     * @param string $sBirthday
121
     * @return int
122
     */
123
    public static function getAgeByDate(string $sBirthday) : int
124
    {
125
        list($iYear, $iMonth, $iDay) = preg_split('/[-.]/', $sBirthday);
126
127
        $aToday = array();
128
        $aToday['mois'] = date('n');
129
        $aToday['jour'] = date('j');
130
        $aToday['annee'] = date('Y');
131
132
        $iYears = $aToday['annee'] - $iYear;
133
134
        if ($aToday['mois'] <= $iMonth) {
135
136
            if ($iMonth == $aToday['mois']) {
137
138
                if ($iDay > $aToday['jour']) { $iYears--; }
139
            }
140
            else {
141
142
                $iYears--;
143
            }
144
        }
145
146
        return $iYears;
147
    }
148
149
    /**
150
     * set name of image
151
     *
152
     * @access public
153
     * @param  int $iWeek number of week
154
     * @param  int $iYear year
155
     * @param string $sFormat
156
     * @return array|Date
157
     */
158
    public static function getMiddleWeek(int $iWeek, int $iYear, string $sFormat = "Y-m-d") : array
159
    {
160
        $iFirstDayInYear = date("N",mktime(0, 0, 0, 1, 1, $iYear));
161
162 View Code Duplication
        if ($iFirstDayInYear < 5) { $iShift = -($iFirstDayInYear - 1) * 86400; }
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
        else { $iShift = (8 - $iFirstDayInYear) * 86400; }
164
165
        if ($iWeek > 1) { $iWeekInSeconds = ($iWeek-1) * 604800; }
166
        else { $iWeekInSeconds = 0; }
167
168
        if (date('N') > 2) {
169
170
            $iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift + 172800;
171
            $iTimestampLastDay = $iTimestamp + 604800;
172
        }
173
        else {
174
175
            $iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift - 432000;
176
            $iTimestampLastDay = $iTimestamp + 604800;
177
        }
178
179
        $aDates = array(date($sFormat, $iTimestamp), date($sFormat, $iTimestampLastDay));
180
181
        if (preg_replace('/^([0-9]+)-[0-9]+-[0-9]+$/', '$1', $aDates[0]) != date('Y')) {
182
183
            $aDates[0] = preg_replace('/^[0-9]+(-[0-9]+-[0-9]+)$/', date('Y').'$1', $aDates[0]);
184
            $aDates[1] = preg_replace('/^[0-9]+(-[0-9]+-[0-9]+)$/', (date('Y')+1).'$1', $aDates[1]);
185
        }
186
187
        return $aDates;
188
    }
189
190
    /**
191
     * set name of image
192
     *
193
     * @access public
194
     * @return array
195
     */
196
    public static function getActualMiddleWeek() : array
197
    {
198
        return self::getMiddleWeek(date('W'), date('Y'));
199
    }
200
201
    /**
202
     * get time of kind "X hour ago"
203
     *
204
     * @access public
205
     * @param  string $sDateTime datetime to convert
206
     * @param  string $sLanguage language
207
     * @return string
208
     */
209
    public static function getTimeAgoInString(string $sDateTime, string $sLanguage = 'fr') : string
210
    {
211
        if ($sLanguage == 'fr') {
212
213
            $sStartReturn = 'Il y a';
214
            $sEndReturn = '';
215
            $sMinutes = 'minute(s) ';
216
            $sHours = 'heure(s) ';
217
            $sDays = 'jour(s) ';
218
            $sMonths = 'mois ';
219
            $sYears = 'années ';
220
        } else {
221
            $sStartReturn = 'Ago';
222
            $sEndReturn = '';
223
            $sMinutes = 'minute(s) ';
224
            $sHours = 'hour(s) ';
225
            $sDays = 'day(s) ';
226
            $sMonths = 'month ';
227
            $sYears = 'years ';
228
        }
229
230
        $oDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $sDateTime);
231
        $iTimeStamp = time() - $oDateTime->getTimestamp();
232
233
        if ($iTimeStamp < 3600) { return $sStartReturn.' '.(int)($iTimeStamp/60).' '.$sMinutes.$sEndReturn; }
234
        if ($iTimeStamp < 86400) { return $sStartReturn.' '.(int)($iTimeStamp/3600).' '.$sHours.$sEndReturn; }
235
        if ($iTimeStamp < 2592000) { return $sStartReturn.' '.(int)($iTimeStamp/86400).' '.$sDays.$sEndReturn; }
236
        if ($iTimeStamp < 31536000) { return $sStartReturn.' '.(int)($iTimeStamp/2592000).' '.$sMonths.$sEndReturn; }
237
        else { return $sStartReturn.' '.(int)($iTimeStamp/31536000).' '.$sYears.$sEndReturn; }
238
    }
239
}
240