Completed
Push — master ( 016764...50d24f )
by judicael
04:08
created

Date::getTimeAgoInString()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 8.6737
cc 6
eloc 16
nc 10
nop 2
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; }
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
		else { $iShift = (8 - $iFirstDayInYear) * 86400; }
48
49
		if ($iWeek > 1) { $iWeekInSeconds = ($iWeek-1) * 604800; }
50
		else { $iWeekInSeconds = 0; }
51
52
		$iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift;
53
		$iTimestampLastDay = mktime(0, 0, 0, 1, 6, $iYear) + $iWeekInSeconds + $iShift + 604800;
54
55
		return array(date($sFormat, $iTimestamp), date($sFormat, $iTimestampLastDay));
56
	}
57
58
	/**
59
	 * set name of image
60
	 *
61
	 * @access public
62
	 * @return \Venus\lib\Date
63
	 */
64
	public static function getActualWeek() : Date
65
	{
66
		return self::getWeek(date('W'), date('Y'));
67
	}
68
69
	/**
70
	 * set name of image
71
	 *
72
	 * @access public
73
	 * @param  string $sMonth number of week
74
	 * @param  string $sLanguage language
75
	 * @return \Venus\lib\Date
76
	 */
77
	public static function getMonthInWord(string $sMonth, string $sLanguage = 'fr') : Date
78
	{
79
		if ($sLanguage == 'fr') {
80
81
			if ($sMonth == '01' || $sMonth == 1) { return 'Janvier'; }
82
			else if ($sMonth == '02' || $sMonth == 2) { return 'Février'; }
83
			else if ($sMonth == '03' || $sMonth == 3) { return 'Mars'; }
84
			else if ($sMonth == '04' || $sMonth == 4) { return 'Avril'; }
85
			else if ($sMonth == '05' || $sMonth == 5) { return 'Mai'; }
86
			else if ($sMonth == '06' || $sMonth == 6) { return 'Juin'; }
87
			else if ($sMonth == '07' || $sMonth == 7) { return 'Juillet'; }
88
			else if ($sMonth == '08' || $sMonth == 8) { return 'Août'; }
89
			else if ($sMonth == '09' || $sMonth == 9) { return 'Septembre'; }
90
			else if ($sMonth == 10) { return 'Octobre'; }
91
			else if ($sMonth == 11) { return 'Novembre'; }
92
			else if ($sMonth == 12) { return 'Décembre'; }
93
		}
94
	}
95
96
	/**
97
	 * set name of image
98
	 *
99
	 * @access public
100
	 * @param  mixed $sDay number of day
101
	 * @param  string $sLanguage language
102
	 * @return \Venus\lib\Date
103
	 */
104
	public static function getDayInWord(string $sDay, string $sLanguage = 'fr') : Date
105
	{
106
		if ($sLanguage == 'fr') {
107
108
			if ($sDay == 0) { return 'dimanche'; }
109
			else if ($sDay == 1) { return 'lundi'; }
110
			else if ($sDay == 2) { return 'mardi'; }
111
			else if ($sDay == 3) { return 'mercredi'; }
112
			else if ($sDay == 4) { return 'jeudi'; }
113
			else if ($sDay == 5) { return 'vendredi'; }
114
			else if ($sDay == 6) { return 'samedi'; }
115
		}
116
	}
117
118
	/**
119
	 * get age by date
120
	 *
121
	 * @access public
122
	 * @param unknown $sBirthday
123
	 * @return int
124
	 */
125
	public static function getAgeByDate(string $sBirthday) : int
126
	{
127
		list($iYear, $iMonth, $iDay) = preg_split('/[-.]/', $sBirthday);
128
129
		$aToday = array();
130
		$aToday['mois'] = date('n');
131
		$aToday['jour'] = date('j');
132
		$aToday['annee'] = date('Y');
133
134
		$iYears = $aToday['annee'] - $iYear;
135
136
		if ($aToday['mois'] <= $iMonth) {
137
138
			if ($iMonth == $aToday['mois']) {
139
140
				if ($iDay > $aToday['jour']) { $iYears--; }
141
			}
142
			else {
143
144
				$iYears--;
145
			}
146
		}
147
148
		return $iYears;
149
	}
150
151
	/**
152
	 * set name of image
153
	 *
154
	 * @access public
155
	 * @param  int $iWeek number of week
156
	 * @param  int $iYear year
157
	 * @return \Venus\lib\Date
158
	 */
159
	public static function getMiddleWeek(int $iWeek, int $iYear, string $sFormat = "Y-m-d") : array
160
	{
161
		$iFirstDayInYear = date("N",mktime(0, 0, 0, 1, 1, $iYear));
162
163 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...
164
		else { $iShift = (8 - $iFirstDayInYear) * 86400; }
165
166
		if ($iWeek > 1) { $iWeekInSeconds = ($iWeek-1) * 604800; }
167
		else { $iWeekInSeconds = 0; }
168
169
		if (date('N') > 2) {
170
171
			$iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift + 172800;
172
			$iTimestampLastDay = $iTimestamp + 604800;
173
		}
174
		else {
175
176
			$iTimestamp = mktime(0, 0, 0, 1, 1, $iYear) + $iWeekInSeconds + $iShift - 432000;
177
			$iTimestampLastDay = $iTimestamp + 604800;
178
		}
179
180
		$aDates = array(date($sFormat, $iTimestamp), date($sFormat, $iTimestampLastDay));
181
182
		if (preg_replace('/^([0-9]+)-[0-9]+-[0-9]+$/', '$1', $aDates[0]) != date('Y')) {
183
184
			$aDates[0] = preg_replace('/^[0-9]+(-[0-9]+-[0-9]+)$/', date('Y').'$1', $aDates[0]);
185
			$aDates[1] = preg_replace('/^[0-9]+(-[0-9]+-[0-9]+)$/', (date('Y')+1).'$1', $aDates[1]);
186
		}
187
188
		return $aDates;
189
	}
190
191
	/**
192
	 * set name of image
193
	 *
194
	 * @access public
195
	 * @param  int $iWeek number of week
0 ignored issues
show
Bug introduced by
There is no parameter named $iWeek. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
196
	 * @param  int $iYear year
0 ignored issues
show
Bug introduced by
There is no parameter named $iYear. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
197
	 * @return array
198
	 */
199
	public static function getActualMiddleWeek() : array
200
	{
201
		return self::getMiddleWeek(date('W'), date('Y'));
202
	}
203
204
	/**
205
	 * get time of kind "X hour ago"
206
	 *
207
	 * @access public
208
	 * @param  string $sDateTime datetime to convert
209
	 * @param  string $sLanguage language
210
	 * @return string
211
	 */
212
	public static function getTimeAgoInString(string $sDateTime, string $sLanguage = 'fr') : string
213
	{
214
		if ($sLanguage == 'fr') {
215
216
			$sStartReturn = 'Il y a';
217
			$sEndReturn = '';
218
			$sMinutes = 'minute(s) ';
219
			$sHours = 'heure(s) ';
220
			$sDays = 'jour(s) ';
221
			$sMonths = 'mois ';
222
			$sYears = 'mois ';
223
		}
224
225
		$oDateTime = DateTime::createFromFormat('Y-m-d H:i:s', $sDateTime);
226
		$iTimeStamp = time() - $oDateTime->getTimestamp();
227
228
		if ($iTimeStamp < 3600) { return $sStartReturn.' '.(int)($iTimeStamp/60).' '.$sMinutes.$sEndReturn; }
0 ignored issues
show
Bug introduced by
The variable $sStartReturn does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $sMinutes does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $sEndReturn does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
229
		if ($iTimeStamp < 86400) { return $sStartReturn.' '.(int)($iTimeStamp/3600).' '.$sHours.$sEndReturn; }
0 ignored issues
show
Bug introduced by
The variable $sHours does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
230
		if ($iTimeStamp < 2592000) { return $sStartReturn.' '.(int)($iTimeStamp/86400).' '.$sDays.$sEndReturn; }
0 ignored issues
show
Bug introduced by
The variable $sDays does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
231
		if ($iTimeStamp < 31536000) { return $sStartReturn.' '.(int)($iTimeStamp/2592000).' '.$sMonths.$sEndReturn; }
0 ignored issues
show
Bug introduced by
The variable $sMonths does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
232
		else { return $sStartReturn.' '.(int)($iTimeStamp/31536000).' '.$sYears.$sEndReturn; }
0 ignored issues
show
Bug introduced by
The variable $sYears does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
233
	}
234
}
235