Completed
Push — master ( 358935...bffbd4 )
by cam
05:38 queued 27s
created

filtres_dates.php ➔ vider_date()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 2
dl 0
loc 17
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Système de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright © avec tendresse depuis 2001                                 *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribué sous licence GNU/GPL.     *
10
 *  Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
/**
14
 * Déclaration de filtres de dates pour les squelettes
15
 *
16
 * @package SPIP\Core\Filtres
17
 **/
18
if (!defined('_ECRIRE_INC_VERSION')) {
19
	return;
20
}
21
22
23
/**
24
 * Extrait une date d'un texte et renvoie le résultat au format de date SQL
25
 *
26
 * L'année et le mois doivent être numériques.
27
 * Le séparateur entre l'année et le mois peut être un `-`, un `:` ou un texte
28
 * quelconque ne contenant pas de chiffres.
29
 *
30
 * Les jours ne sont pas pris en compte et le résultat est toujours le 1er du mois.
31
 *
32
 * @link https://www.spip.net/5516
33
 * @param string $texte
34
 *    Texte contenant une date tel que `2008-04`
35
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
36
 *    Date au format SQL tel que `2008-04-01`
37
 **/
38
function extraire_date($texte) {
39
	// format = 2001-08
40
	if (preg_match(",([1-2][0-9]{3})[^0-9]*(1[0-2]|0?[1-9]),", $texte, $regs)) {
41
		return $regs[1] . "-" . sprintf("%02d", $regs[2]) . "-01";
42
	}
43
}
44
45
46
/**
47
 * Normaliser une date vers le format datetime (Y-m-d H:i:s)
48
 *
49
 * @note
50
 *     Si elle vient du contexte (public/parametrer.php), on force le jour
51
 *
52
 * @filtre
53
 * @link https://www.spip.net/5518
54
 * @uses vider_date()
55
 * @param string $date
56
 *     La date à normaliser
57
 * @param bool $forcer_jour
58
 *     true pour forcer à indiquer un jour et mois (01) s'il n'y en a pas.
59
 * @return string
60
 *     - une date au format datetime
61
 *     - une chaîne vide si la date est considérée nulle
62
 **/
63
function normaliser_date($date, $forcer_jour = false) {
64
	$date = vider_date($date);
65
	if ($date) {
66
		if (preg_match("/^[0-9]{8,10}$/", $date)) {
67
			$date = date("Y-m-d H:i:s", $date);
68
		}
69
		if (preg_match("#^([12][0-9]{3})([-/]00)?( [-0-9:]+)?$#", $date, $regs)) {
70
			$regs = array_pad($regs, 4, null); // eviter notice php
71
			$date = $regs[1] . "-00-00" . $regs[3];
72
		} else {
73
			if (preg_match("#^([12][0-9]{3}[-/][01]?[0-9])([-/]00)?( [-0-9:]+)?$#", $date, $regs)) {
74
				$regs = array_pad($regs, 4, null); // eviter notice php
75
				$date = preg_replace("@/@", "-", $regs[1]) . "-00" . $regs[3];
76
			} else {
77
				$date = date("Y-m-d H:i:s", strtotime($date));
78
			}
79
		}
80
81
		if ($forcer_jour) {
82
			$date = str_replace('-00', '-01', $date);
83
		}
84
	}
85
86
	return $date;
87
}
88
89
/**
90
 * Enlève une date considérée comme vide
91
 *
92
 * @param string $letexte
93
 * @param bool $verif_format_date
94
 * @return string
95
 *     - La date entrée (si elle n'est pas considérée comme nulle)
96
 *     - Une chaine vide
97
 **/
98
function vider_date($letexte, $verif_format_date = false) {
99
	if (!$verif_format_date
100
	  or (in_array(strlen($letexte), [10,19]) and
101
			  preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}(\s[0-9]{2}:[0-9]{2}:[0-9]{2})?$/", $letexte))) {
102
103
		if (strncmp("0000-00-00", $letexte, 10) == 0) {
104
			return '';
105
		}
106
		if (strncmp("0001-01-01", $letexte, 10) == 0) {
107
			return '';
108
		}
109
		if (strncmp("1970-01-01", $letexte, 10) == 0) {
110
			return '';
111
		}  // eviter le bug GMT-1
112
	}
113
	return $letexte;
114
}
115
116
/**
117
 * Retrouve à partir d'une chaîne les valeurs heures, minutes, secondes
118
 *
119
 * Retrouve une horaire au format `11:29:55`
120
 *
121
 * @param string $date
122
 *     Chaîne de date contenant éventuellement une horaire
123
 * @return array
124
 *     - [heures, minutes, secondes] si horaire trouvée
125
 *     - [0, 0, 0] sinon
126
 **/
127
function recup_heure($date) {
128
129
	static $d = array(0, 0, 0);
130
	if (!preg_match('#([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $date, $r)) {
131
		return $d;
132
	}
133
134
	array_shift($r);
135
136
	return $r;
137
}
138
139
/**
140
 * Retourne l'heure d'une date
141
 *
142
 * @filtre
143
 * @link https://www.spip.net/4293
144
 * @uses recup_heure()
145
 *
146
 * @param string $numdate La date à extraire
147
 * @return int heures, sinon 0
148
 **/
149 View Code Duplication
function heures($numdate) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
150
	$date_array = recup_heure($numdate);
151
	if ($date_array) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date_array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
152
		list($heures, $minutes, $secondes) = $date_array;
0 ignored issues
show
Unused Code introduced by
The assignment to $minutes is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $secondes is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
153
	}
154
155
	return $heures;
0 ignored issues
show
Bug introduced by
The variable $heures 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...
156
}
157
158
/**
159
 * Retourne les minutes d'une date
160
 *
161
 * @filtre
162
 * @link https://www.spip.net/4300
163
 * @uses recup_heure()
164
 *
165
 * @param string $numdate La date à extraire
166
 * @return int minutes, sinon 0
167
 **/
168 View Code Duplication
function minutes($numdate) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
169
	$date_array = recup_heure($numdate);
170
	if ($date_array) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date_array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
171
		list($heures, $minutes, $secondes) = $date_array;
0 ignored issues
show
Unused Code introduced by
The assignment to $secondes is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $heures is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
172
	}
173
174
	return $minutes;
0 ignored issues
show
Bug introduced by
The variable $minutes 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...
175
}
176
177
/**
178
 * Retourne les secondes d'une date
179
 *
180
 * @filtre
181
 * @link https://www.spip.net/4312
182
 * @uses recup_heure()
183
 *
184
 * @param string $numdate La date à extraire
185
 * @return int secondes, sinon 0
186
 **/
187 View Code Duplication
function secondes($numdate) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
188
	$date_array = recup_heure($numdate);
189
	if ($date_array) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date_array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
190
		list($heures, $minutes, $secondes) = $date_array;
0 ignored issues
show
Unused Code introduced by
The assignment to $heures is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $minutes is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
191
	}
192
193
	return $secondes;
0 ignored issues
show
Bug introduced by
The variable $secondes 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...
194
}
195
196
/**
197
 * Retourne l'horaire (avec minutes) d'une date, tel que `12h36min`
198
 *
199
 * @note
200
 *     Le format de retour varie selon la langue utilisée.
201
 *
202
 * @filtre
203
 * @link https://www.spip.net/5519
204
 *
205
 * @param string $numdate La date à extraire
206
 * @param string $forme.
0 ignored issues
show
Documentation introduced by
There is no parameter named $forme.. Did you maybe mean $forme?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
207
 *	- si vide,  précise l'unité des minutes : 12h10min
208
 *	- si 'abbr' ne précise pas l'unité des minutes : 12h10
209
 * @return string L'heure formatée dans la langue en cours.
210
 **/
211
function heures_minutes($numdate, $forme='') {
212
	if ($forme !='abbr') {
213
		return _T('date_fmt_heures_minutes', array('h' => heures($numdate), 'm' => minutes($numdate)));
214
	}
215
	else {
216
		return _T('date_fmt_heures_minutes_court', array('h' => heures($numdate), 'm' => minutes($numdate)));
217
	}
218
}
219
220
/**
221
 * Retrouve à partir d'une date les valeurs année, mois, jour, heures, minutes, secondes
222
 *
223
 * Annee, mois, jour sont retrouvés si la date contient par exemple :
224
 * - '03/11/2015', '3/11/15'
225
 * - '2015-11-04', '2015-11-4'
226
 * - '2015-11'
227
 *
228
 * Dans ces cas, les heures, minutes, secondes sont retrouvés avec `recup_heure()`
229
 *
230
 * Annee, mois, jour, heures, minutes, secondes sont retrouvés si la date contient par exemple :
231
 * - '20151104111420'
232
 *
233
 * @uses recup_heure()
234
 *
235
 * @param string $numdate La date à extraire
236
 * @param bool $forcer_jour
237
 *     True pour tout le temps renseigner un jour ou un mois (le 1) s'il
238
 *     ne sont pas indiqués dans la date.
239
 * @return array [année, mois, jour, heures, minutes, secondes]
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
240
 **/
241
function recup_date($numdate, $forcer_jour = true) {
242
	if (!$numdate) {
243
		return '';
244
	}
245
	$heures = $minutes = $secondes = 0;
246
	if (preg_match('#([0-9]{1,2})/([0-9]{1,2})/([0-9]{4}|[0-9]{1,2})#', $numdate, $regs)) {
247
		$jour = $regs[1];
248
		$mois = $regs[2];
249
		$annee = $regs[3];
250
		if ($annee < 90) {
251
			$annee = 2000 + $annee;
252
		} elseif ($annee < 100) {
253
			$annee = 1900 + $annee;
254
		}
255
		list($heures, $minutes, $secondes) = recup_heure($numdate);
256
257
	} elseif (preg_match('#([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})#', $numdate, $regs)) {
258
		$annee = $regs[1];
259
		$mois = $regs[2];
260
		$jour = $regs[3];
261
		list($heures, $minutes, $secondes) = recup_heure($numdate);
262
	} elseif (preg_match('#([0-9]{4})-([0-9]{2})#', $numdate, $regs)) {
263
		$annee = $regs[1];
264
		$mois = $regs[2];
265
		$jour = '';
266
		list($heures, $minutes, $secondes) = recup_heure($numdate);
267
	} elseif (preg_match('#^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$#', $numdate, $regs)) {
268
		$annee = $regs[1];
269
		$mois = $regs[2];
270
		$jour = $regs[3];
271
		$heures = $regs[4];
272
		$minutes = $regs[5];
273
		$secondes = $regs[6];
274
	} else {
275
		$annee = $mois = $jour = '';
276
	}
277
	if ($annee > 4000) {
278
		$annee -= 9000;
279
	}
280 View Code Duplication
	if (strlen($jour) and substr($jour, 0, 1) == '0') {
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...
281
		$jour = substr($jour, 1);
282
	}
283
284
	if ($forcer_jour and $jour == '0') {
285
		$jour = '1';
286
	}
287
	if ($forcer_jour and $mois == '0') {
288
		$mois = '1';
289
	}
290
	if ($annee or $mois or $jour or $heures or $minutes or $secondes) {
291
		return array($annee, $mois, $jour, $heures, $minutes, $secondes);
292
	}
293
}
294
295
/**
296
 * Retourne une date relative si elle est récente, sinon une date complète
297
 *
298
 * En fonction de la date transmise, peut retourner par exemple :
299
 * - «il y a 3 minutes»,
300
 * - «il y a 11 heures»,
301
 * - «10 mai 2015 à 10h23min»
302
 *
303
 * @example `[(#DATE|date_interface)]`
304
 *
305
 * @filtre
306
 * @link https://www.spip.net/5520
307
 * @uses date_relative()
308
 * @uses affdate_heure() utilisé si le décalage est trop grand
309
 *
310
 * @param string $date
311
 *     La date fournie
312
 * @param int $decalage_maxi
313
 *     Durée écoulée, en secondes, à partir de laquelle on bascule sur une date complète.
314
 *     Par défaut +/- 12h.
315
 * @return string
316
 *     La date relative ou complète
317
 **/
318
function date_interface($date, $decalage_maxi = 43200 /* 12*3600 */) {
319
	return sinon(
320
		date_relative($date, $decalage_maxi),
321
		affdate_heure($date)
322
	);
323
}
324
325
/**
326
 * Retourne une date relative (passée ou à venir)
327
 *
328
 * En fonction de la date transmise ainsi que de la date de référence
329
 * (par défaut la date actuelle), peut retourner par exemple :
330
 * - «il y a 3 minutes»,
331
 * - «il y a 2 semmaines»,
332
 * - «dans 1 semaine»
333
 *
334
 * @example
335
 *     - `[(#DATE|date_relative)]`
336
 *     - `[(#DATE|date_relative{43200})]`
337
 *     - `[(#DATE|date_relative{0, #AUTRE_DATE})]` Calcul relatif à une date spécifique
338
 *
339
 * @filtre
340
 * @link https://www.spip.net/4277
341
 *
342
 * @param string $date
343
 *     La date fournie
344
 * @param int $decalage_maxi
345
 *     Durée écoulée, en secondes, au delà de laquelle on ne retourne pas de date relative
346
 *     Indiquer `0` (par défaut) pour ignorer.
347
 * @param string $ref_date
0 ignored issues
show
Documentation introduced by
Should the type for parameter $ref_date not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
348
 *     La date de référence pour le calcul relatif, par défaut la date actuelle
349
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
350
 *     - La date relative
351
 *     - "" si un dépasse le décalage maximum est indiqué et dépassé.
352
 **/
353
function date_relative($date, $decalage_maxi = 0, $ref_date = null) {
354
355
	if (is_null($ref_date)) {
356
		$ref_time = time();
357
	} else {
358
		$ref_time = strtotime($ref_date);
359
	}
360
361
	if (!$date) {
362
		return;
363
	}
364
	$decal = date("U", $ref_time) - date("U", strtotime($date));
365
366
	if ($decalage_maxi and ($decal > $decalage_maxi or $decal < 0)) {
367
		return '';
368
	}
369
370
	if ($decal < 0) {
371
		$il_y_a = "date_dans";
372
		$decal = -1 * $decal;
373
	} else {
374
		$il_y_a = "date_il_y_a";
375
	}
376
377
	if ($decal > 3600 * 24 * 30 * 6) {
378
		return affdate_court($date);
379
	}
380
381
	if ($decal > 3600 * 24 * 30) {
382
		$mois = floor($decal / (3600 * 24 * 30));
383
		if ($mois < 2) {
384
			$delai = "$mois " . _T("date_un_mois");
385
		} else {
386
			$delai = "$mois " . _T("date_mois");
387
		}
388
	} else {
389
		if ($decal > 3600 * 24 * 7) {
390
			$semaines = floor($decal / (3600 * 24 * 7));
391
			if ($semaines < 2) {
392
				$delai = "$semaines " . _T("date_une_semaine");
393
			} else {
394
				$delai = "$semaines " . _T("date_semaines");
395
			}
396
		} else {
397
			if ($decal > 3600 * 24) {
398
				$jours = floor($decal / (3600 * 24));
399
				if ($jours < 2) {
400
					return $il_y_a == "date_dans" ? _T("date_demain") : _T("date_hier");
401
				} else {
402
					$delai = "$jours " . _T("date_jours");
403
				}
404
			} else {
405
				if ($decal >= 3600) {
406
					$heures = floor($decal / 3600);
407
					if ($heures < 2) {
408
						$delai = "$heures " . _T("date_une_heure");
409
					} else {
410
						$delai = "$heures " . _T("date_heures");
411
					}
412
				} else {
413
					if ($decal >= 60) {
414
						$minutes = floor($decal / 60);
415
						if ($minutes < 2) {
416
							$delai = "$minutes " . _T("date_une_minute");
417
						} else {
418
							$delai = "$minutes " . _T("date_minutes");
419
						}
420
					} else {
421
						$secondes = ceil($decal);
422
						if ($secondes < 2) {
423
							$delai = "$secondes " . _T("date_une_seconde");
424
						} else {
425
							$delai = "$secondes " . _T("date_secondes");
426
						}
427
					}
428
				}
429
			}
430
		}
431
	}
432
433
	return _T($il_y_a, array("delai" => $delai));
434
}
435
436
437
/**
438
 * Retourne une date relative courte (passée ou à venir)
439
 *
440
 * Retourne «hier», «aujourd'hui» ou «demain» si la date correspond, sinon
441
 * utilise `date_relative()`
442
 *
443
 * @example `[(#DATE|date_relativecourt)]`
444
 *
445
 * @filtre
446
 * @uses date_relative()
447
 *
448
 * @param string $date
449
 *     La date fournie
450
 * @param int $decalage_maxi
451
 *     Durée écoulée, en secondes, au delà de laquelle on ne retourne pas de date relative
452
 *     Indiquer `0` (par défaut) pour ignorer.
453
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
454
 *     - La date relative
455
 *     - "" si un dépasse le décalage maximum est indiqué et dépassé.
456
 **/
457
function date_relativecourt($date, $decalage_maxi = 0) {
458
459
	if (!$date) {
460
		return;
461
	}
462
	$decal = date("U", strtotime(date('Y-m-d')) - strtotime(date('Y-m-d', strtotime($date))));
463
464
	if ($decalage_maxi and ($decal > $decalage_maxi or $decal < 0)) {
465
		return '';
466
	}
467
468
	if ($decal < -24 * 3600) {
469
		$retour = date_relative($date, $decalage_maxi);
470
	} elseif ($decal < 0) {
471
		$retour = _T("date_demain");
472
	} else {
473
		if ($decal < (3600 * 24)) {
474
			$retour = _T("date_aujourdhui");
475
		} else {
476
			if ($decal < (3600 * 24 * 2)) {
477
				$retour = _T("date_hier");
478
			} else {
479
				$retour = date_relative($date, $decalage_maxi);
480
			}
481
		}
482
	}
483
484
	return $retour;
485
}
486
487
/**
488
 * Formatage humain de la date `$numdate` selon le format `$vue`
489
 *
490
 * @param string $numdate
491
 *     Une écriture de date
492
 * @param string $vue
493
 *     Type de format souhaité ou expression pour `strtotime()` tel que `Y-m-d h:i:s`
494
 * @param array $options {
495
 * @type string $param
496
 *         'abbr' ou 'initiale' permet d'afficher les jours au format court ou initiale
497
 * @type int $annee_courante
498
 *         Permet de definir l'annee de reference pour l'affichage des dates courtes
499
 * }
500
 * @return mixed|string
501
 */
502
function affdate_base($numdate, $vue, $options = array()) {
503
	if (is_string($options)) {
504
		$options = array('param' => $options);
505
	}
506
	$date_array = recup_date($numdate, false);
507
	if (!$date_array) {
508
		return;
509
	}
510
	list($annee, $mois, $jour, $heures, $minutes, $secondes) = $date_array;
511
512
	// 1er, 21st, etc.
513
	$journum = $jour;
514
515
	if ($jour == 0) {
516
		$jour = '';
517
		$njour = 0;
518
	} else {
519
		$njour = intval($jour);
520
		if ($jourth = _T('date_jnum' . $jour)) {
521
			$jour = $jourth;
522
		}
523
	}
524
525
	$mois = intval($mois);
526
	if ($mois > 0 and $mois < 13) {
527
		$nommois = _T('date_mois_' . $mois);
528
		if ($jour) {
529
			$jourmois = _T('date_de_mois_' . $mois, array('j' => $jour, 'nommois' => $nommois));
530
		} else {
531
			$jourmois = $nommois;
532
		}
533
	} else {
534
		$nommois = '';
535
		$jourmois = '';
536
	}
537
538
	if ($annee < 0) {
539
		$annee = -$annee . " " . _T('date_avant_jc');
540
		$avjc = true;
541
	} else {
542
		$avjc = false;
543
	}
544
545
	switch ($vue) {
546
		case 'saison':
547
		case 'saison_annee':
548
			$saison = '';
549
			if ($mois > 0) {
550
				$saison = ($options['param'] == 'sud') ? 3 : 1;
551 View Code Duplication
				if (($mois == 3 and $jour >= 21) or $mois > 3) {
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...
552
					$saison = ($options['param'] == 'sud') ? 4 : 2;
553
				}
554 View Code Duplication
				if (($mois == 6 and $jour >= 21) or $mois > 6) {
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...
555
					$saison = ($options['param'] == 'sud') ? 1 : 3;
556
				}
557 View Code Duplication
				if (($mois == 9 and $jour >= 21) or $mois > 9) {
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...
558
					$saison = ($options['param'] == 'sud') ? 2 : 4;
559
				}
560 View Code Duplication
				if (($mois == 12 and $jour >= 21) or $mois > 12) {
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...
561
					$saison = ($options['param'] == 'sud') ? 3 : 1;
562
				}
563
			}
564
			if ($vue == 'saison') {
565
				return $saison ? _T('date_saison_' . $saison) : '';
566
			} else {
567
				return $saison ? trim(_T('date_fmt_saison_annee',
568
					array('saison' => _T('date_saison_' . $saison), 'annee' => $annee))) : '';
569
			}
570
571
		case 'court':
572
			if ($avjc) {
573
				return $annee;
574
			}
575
			$a = ((isset($options['annee_courante']) and $options['annee_courante']) ? $options['annee_courante'] : date('Y'));
576
			if ($annee < ($a - 100) or $annee > ($a + 100)) {
577
				return $annee;
578
			}
579 View Code Duplication
			if ($annee != $a) {
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...
580
				return _T('date_fmt_mois_annee',
581
					array('mois' => $mois, 'nommois' => spip_ucfirst($nommois), 'annee' => $annee));
582
			}
583
584
			return _T('date_fmt_jour_mois',
585
				array('jourmois' => $jourmois, 'jour' => $jour, 'mois' => $mois, 'nommois' => $nommois, 'annee' => $annee));
586
587
		case 'jourcourt':
588
			if ($avjc) {
589
				return $annee;
590
			}
591
			$a = ((isset($options['annee_courante']) and $options['annee_courante']) ? $options['annee_courante'] : date('Y'));
592
			if ($annee < ($a - 100) or $annee > ($a + 100)) {
593
				return $annee;
594
			}
595
			if ($annee != $a) {
596
				return _T('date_fmt_jour_mois_annee',
597
					array('jourmois' => $jourmois, 'jour' => $jour, 'mois' => $mois, 'nommois' => $nommois, 'annee' => $annee));
598
			}
599
600
			return _T('date_fmt_jour_mois',
601
				array('jourmois' => $jourmois, 'jour' => $jour, 'mois' => $mois, 'nommois' => $nommois, 'annee' => $annee));
602
603
		case 'entier':
604
			if ($avjc) {
605
				return $annee;
606
			}
607
			if ($jour) {
608
				return _T('date_fmt_jour_mois_annee',
609
					array('jourmois' => $jourmois, 'jour' => $jour, 'mois' => $mois, 'nommois' => $nommois, 'annee' => $annee));
610 View Code Duplication
			} elseif ($mois) {
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...
611
				return trim(_T('date_fmt_mois_annee', array('mois' => $mois, 'nommois' => $nommois, 'annee' => $annee)));
612
			} else {
613
				return $annee;
614
			}
615
616
		case 'nom_mois':
617
			$param = ((isset($options['param']) and $options['param']) ? '_' . $options['param'] : '');
618
			if ($param and $mois) {
619
				return _T('date_mois_' . $mois . $param);
620
			}
621
622
			return $nommois;
623
624
		case 'mois':
625
			return sprintf("%02s", $mois);
626
627
		case 'jour':
628
			return $jour;
629
630
		case 'journum':
631
			return $journum;
632
633
		case 'nom_jour':
634
			if (!$mois or !$njour) {
635
				return '';
636
			}
637
			$nom = mktime(1, 1, 1, $mois, $njour, $annee);
638
			$nom = 1 + date('w', $nom);
639
			$param = ((isset($options['param']) and $options['param']) ? '_' . $options['param'] : '');
640
641
			return _T('date_jour_' . $nom . $param);
642
643
		case 'mois_annee':
644
			if ($avjc) {
645
				return $annee;
646
			}
647
648
			return trim(_T('date_fmt_mois_annee', array('mois' => $mois, 'nommois' => $nommois, 'annee' => $annee)));
649
650
		case 'annee':
651
			return $annee;
652
653
		// Cas d'une vue non definie : retomber sur le format
654
		// de date propose par http://www.php.net/date
655
		default:
656
			list($annee, $mois, $jour, $heures, $minutes, $secondes) = $date_array;
657
			if (!$time = mktime($heures, $minutes, $secondes, $mois, $jour, $annee)) {
658
				$time = strtotime($numdate);
659
			}
660
			return date($vue, $time);
661
	}
662
}
663
664
665
/**
666
 * Affiche le nom du jour pour une date donnée
667
 *
668
 * @example
669
 *     - `[(#DATE|nom_jour)]` lundi
670
 *     - `[(#DATE|nom_jour{abbr})]` lun.
671
 *     - `[(#DATE|nom_jour{initiale})]` l.
672
 *
673
 * @filtre
674
 * @link https://www.spip.net/4305
675
 * @uses affdate_base()
676
 *
677
 * @param string $numdate
678
 *     Une écriture de date
679
 * @param string $forme
680
 *     Forme spécifique de retour :
681
 *     - initiale : l'initiale du jour
682
 *     - abbr : abbréviation du jour
683
 *     - '' : le nom complet (par défaut)
684
 * @return string
685
 *     Nom du jour
686
 **/
687
function nom_jour($numdate, $forme = '') {
688
	if (!($forme == 'abbr' or $forme == 'initiale')) {
689
		$forme = '';
690
	}
691
692
	return affdate_base($numdate, 'nom_jour', $forme);
693
}
694
695
/**
696
 * Affiche le numéro du jour (1er à 31) pour une date donnée
697
 *
698
 * Utilise une abbréviation (exemple "1er") pour certains jours,
699
 * en fonction de la langue utilisée.
700
 *
701
 * @example `[(#DATE|jour)]`
702
 *
703
 * @filtre
704
 * @link https://www.spip.net/4295
705
 * @uses affdate_base()
706
 * @see  journum()
707
 *
708
 * @param string $numdate
709
 *     Une écriture de date
710
 * @return int
711
 *     Numéro du jour
712
 **/
713
function jour($numdate) {
714
	return affdate_base($numdate, 'jour');
715
}
716
717
/**
718
 * Affiche le numéro du jour (1 à 31) pour une date donnée
719
 *
720
 * @example `[(#DATE|journum)]`
721
 *
722
 * @filtre
723
 * @uses affdate_base()
724
 * @see  jour()
725
 *
726
 * @param string $numdate
727
 *     Une écriture de date
728
 * @return int
729
 *     Numéro du jour
730
 **/
731
function journum($numdate) {
732
	return affdate_base($numdate, 'journum');
733
}
734
735
/**
736
 * Affiche le numéro du mois (01 à 12) pour une date donnée
737
 *
738
 * @example `[(#DATE|mois)]`
739
 *
740
 * @filtre
741
 * @link https://www.spip.net/4303
742
 * @uses affdate_base()
743
 *
744
 * @param string $numdate
745
 *     Une écriture de date
746
 * @return string
747
 *     Numéro du mois (sur 2 chiffres)
748
 **/
749
function mois($numdate) {
750
	return affdate_base($numdate, 'mois');
751
}
752
753
/**
754
 * Affiche le nom du mois pour une date donnée
755
 *
756
 * @example
757
 *     - `[(#DATE|nom_mois)]` novembre
758
 *     - `[(#DATE|nom_mois{abbr})]` nov.
759
 *
760
 * @filtre
761
 * @link https://www.spip.net/4306
762
 * @uses affdate_base()
763
 *
764
 * @param string $numdate
765
 *     Une écriture de date
766
 * @param string $forme
767
 *     Forme spécifique de retour :
768
 *     - abbr : abbréviation du mois
769
 *     - '' : le nom complet (par défaut)
770
 * @return string
771
 *     Nom du mois
772
 **/
773
function nom_mois($numdate, $forme = '') {
774
	if (!($forme == 'abbr')) {
775
		$forme = '';
776
	}
777
778
	return affdate_base($numdate, 'nom_mois', $forme);
779
}
780
781
/**
782
 * Affiche l'année sur 4 chiffres d'une date donnée
783
 *
784
 * @example `[(#DATE|annee)]`
785
 *
786
 * @filtre
787
 * @link https://www.spip.net/4146
788
 * @uses affdate_base()
789
 *
790
 * @param string $numdate
791
 *     Une écriture de date
792
 * @return int
793
 *     Année (sur 4 chiffres)
794
 **/
795
function annee($numdate) {
796
	return affdate_base($numdate, 'annee');
797
}
798
799
800
/**
801
 * Affiche le nom boréal ou austral de la saison
802
 *
803
 * @filtre
804
 * @link https://www.spip.net/4311
805
 * @uses affdate_base()
806
 * @example
807
 *     En PHP
808
 *     ```
809
 *     saison("2008-10-11 14:08:45") affiche "automne"
810
 *     saison("2008-10-11 14:08:45", "sud") affiche "printemps"
811
 *     ```
812
 *     En squelettes
813
 *     ```
814
 *     [(#DATE|saison)]
815
 *     [(#DATE|saison{sud})]
816
 *     ```
817
 *
818
 * @param string $numdate
819
 *     Une écriture de date
820
 * @param string $hemisphere
821
 *     Nom optionnel de l'hémisphère (sud ou nord) ; par défaut nord
822
 * @return string
823
 *     La date formatée
824
 **/
825
function saison($numdate, $hemisphere = 'nord') {
826
	if ($hemisphere != 'sud') {
827
		$hemisphere = 'nord';
828
	}
829
830
	return affdate_base($numdate, 'saison', $hemisphere);
831
}
832
833
834
/**
835
 * Affiche le nom boréal ou austral de la saison suivi de l'année en cours
836
 *
837
 * @filtre
838
 * @uses affdate_base()
839
 * @example
840
 *     En PHP
841
 *     ```
842
 *     saison_annee("2008-10-11 14:08:45") affiche "automne 2008"
843
 *     saison_annee("2008-10-11 14:08:45", "sud") affiche "printemps 2008"
844
 *     ```
845
 *     En squelettes
846
 *     ```
847
 *     [(#DATE|saison_annee)]
848
 *     [(#DATE|saison_annee{sud})]
849
 *     ```
850
 *
851
 * @param string $numdate
852
 *     Une écriture de date
853
 * @param string $hemisphere
854
 *     Nom optionnel de l'hémisphère (sud ou nord) ; par défaut nord
855
 * @return string
856
 *     La date formatée
857
 **/
858
function saison_annee($numdate, $hemisphere = 'nord') {
859
	if ($hemisphere != 'sud') {
860
		$hemisphere = 'nord';
861
	}
862
863
	return affdate_base($numdate, 'saison_annee', $hemisphere);
864
}
865
866
/**
867
 * Formate une date
868
 *
869
 * @example
870
 *     En PHP`affdate("2008-10-11 14:08:45")` affiche "11 octobre 2008"
871
 *
872
 * @example
873
 *     En squelettes
874
 *     - `[(#DATE|affdate)]`
875
 *     - `[(#DATE|affdate{Y-m-d})]`
876
 *
877
 * @filtre
878
 * @link https://www.spip.net/4129
879
 * @uses affdate_base()
880
 * @see  affdate_court()
881
 * @see  affdate_jourcourt()
882
 *
883
 * @param string $numdate
884
 *     Une écriture de date
885
 * @param string $format
886
 *     Type de format souhaité ou expression pour `strtotime()` tel que `Y-m-d h:i:s`
887
 * @return string
888
 *     La date formatée
889
 **/
890
function affdate($numdate, $format = 'entier') {
891
	return affdate_base($numdate, $format);
892
}
893
894
895
/**
896
 * Formate une date, omet l'année si année courante, sinon omet le jour
897
 *
898
 * Si l'année actuelle (ou indiquée dans `$annee_courante`) est 2015,
899
 * retournera "21 juin" si la date en entrée est le 21 juin 2015,
900
 * mais retournera "juin 2013" si la date en entrée est le 21 juin 2013.
901
 *
902
 * @example `[(#DATE|affdate_court)]`
903
 *
904
 * @filtre
905
 * @link https://www.spip.net/4130
906
 * @uses affdate_base()
907
 * @see  affdate()
908
 * @see  affdate_jourcourt()
909
 *
910
 * @param string $numdate
911
 *     Une écriture de date
912
 * @param int|null $annee_courante
913
 *     L'année de comparaison, utilisera l'année en cours si omis.
914
 * @return string
915
 *     La date formatée
916
 **/
917
function affdate_court($numdate, $annee_courante = null) {
918
	return affdate_base($numdate, 'court', array('annee_courante' => $annee_courante));
919
}
920
921
922
/**
923
 * Formate une date, omet l'année si année courante
924
 *
925
 * Si l'année actuelle (ou indiquée dans `$annee_courante`) est 2015,
926
 * retournera "21 juin" si la date en entrée est le 21 juin 2015,
927
 * mais retournera "21 juin 2013" si la date en entrée est le 21 juin 2013.
928
 *
929
 * @example `[(#DATE|affdate_jourcourt)]`
930
 *
931
 * @filtre
932
 * @link https://www.spip.net/4131
933
 * @uses affdate_base()
934
 * @see  affdate()
935
 * @see  affdate_court()
936
 *
937
 * @param string $numdate
938
 *     Une écriture de date
939
 * @param int|null $annee_courante
940
 *     L'année de comparaison, utilisera l'année en cours si omis.
941
 * @return string
942
 *     La date formatée
943
 **/
944
function affdate_jourcourt($numdate, $annee_courante = null) {
945
	return affdate_base($numdate, 'jourcourt', array('annee_courante' => $annee_courante));
946
}
947
948
/**
949
 * Retourne le mois en toute lettre et l’année d'une date
950
 *
951
 * Ne retourne pas le jour donc.
952
 *
953
 * @filtre
954
 * @link https://www.spip.net/4132
955
 * @uses affdate_base()
956
 *
957
 * @param string $numdate
958
 *     Une écriture de date
959
 * @return string
960
 *     La date formatée
961
 **/
962
function affdate_mois_annee($numdate) {
963
	return affdate_base($numdate, 'mois_annee');
964
}
965
966
/**
967
 * Retourne la date suivie de l'heure
968
 *
969
 * @example `[(#DATE|affdate_heure)]` peut donner "11 novembre 2015 à 11h10min"
970
 *
971
 * @filtre
972
 * @uses recup_date()
973
 * @uses affdate()
974
 *
975
 * @param string $numdate
976
 *     Une écriture de date
977
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
978
 *     La date formatée
979
 **/
980
function affdate_heure($numdate) {
981
	$date_array = recup_date($numdate);
982
	if (!$date_array) {
983
		return;
984
	}
985
	list($annee, $mois, $jour, $heures, $minutes, $sec) = $date_array;
0 ignored issues
show
Unused Code introduced by
The assignment to $sec is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $annee is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $mois is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $jour is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
986
987
	return _T('date_fmt_jour_heure', array(
988
		'jour' => affdate($numdate),
989
		'heure' => _T('date_fmt_heures_minutes', array('h' => $heures, 'm' => $minutes))
990
	));
991
}
992
993
/**
994
 * Afficher de facon textuelle les dates de début et fin en fonction des cas
995
 *
996
 * - Lundi 20 fevrier a 18h
997
 * - Le 20 fevrier de 18h a 20h
998
 * - Du 20 au 23 fevrier
999
 * - Du 20 fevrier au 30 mars
1000
 * - Du 20 fevrier 2007 au 30 mars 2008
1001
 *
1002
 * `$horaire='oui'` ou `true` permet d'afficher l'horaire,
1003
 * toute autre valeur n'indique que le jour
1004
 * `$forme` peut contenir une ou plusieurs valeurs parmi
1005
 *  - `abbr` (afficher le nom des jours en abrege)
1006
 *  - `hcal` (generer une date au format hcal)
1007
 *  - `jour` (forcer l'affichage des jours)
1008
 *  - `annee` (forcer l'affichage de l'annee)
1009
 *
1010
 * @param string $date_debut
1011
 * @param string $date_fin
1012
 * @param string $horaire
1013
 * @param string $forme
1014
 *   - `abbr` pour afficher le nom du jour en abrege (Dim. au lieu de Dimanche)
1015
 *   - `annee` pour forcer l'affichage de l'annee courante
1016
 *   - `jour` pour forcer l'affichage du nom du jour
1017
 *   - `hcal` pour avoir un markup microformat abbr
1018
 * @return string
1019
 *     Texte de la date
1020
 */
1021
function affdate_debut_fin($date_debut, $date_fin, $horaire = 'oui', $forme = '') {
1022
	$abbr = $jour = '';
1023
	$affdate = "affdate_jourcourt";
1024
	if (strpos($forme, 'abbr') !== false) {
1025
		$abbr = 'abbr';
1026
	}
1027
	if (strpos($forme, 'annee') !== false) {
1028
		$affdate = 'affdate';
1029
	}
1030
	if (strpos($forme, 'jour') !== false) {
1031
		$jour = 'jour';
1032
	}
1033
1034
	$dtstart = $dtend = $dtabbr = "";
1035
	if (strpos($forme, 'hcal') !== false) {
1036
		$dtstart = "<abbr class='dtstart' title='" . date_iso($date_debut) . "'>";
1037
		$dtend = "<abbr class='dtend' title='" . date_iso($date_fin) . "'>";
1038
		$dtabbr = "</abbr>";
1039
	}
1040
1041
	$date_debut = strtotime($date_debut);
1042
	$date_fin = strtotime($date_fin);
1043
	$d = date("Y-m-d", $date_debut);
1044
	$f = date("Y-m-d", $date_fin);
1045
	$h = ($horaire === 'oui' or $horaire === true);
1046
	$hd = _T('date_fmt_heures_minutes_court', array('h' => date("H", $date_debut), 'm' => date("i", $date_debut)));
1047
	$hf = _T('date_fmt_heures_minutes_court', array('h' => date("H", $date_fin), 'm' => date("i", $date_fin)));
1048
1049
	if ($d == $f) { // meme jour
1050
		$nomjour = nom_jour($d, $abbr);
1051
		$s = $affdate($d);
1052
		$s = _T('date_fmt_jour', array('nomjour' => $nomjour, 'jour' => $s));
1053
		if ($h) {
1054
			if ($hd == $hf) {
1055
				// Lundi 20 fevrier a 18h25
1056
				$s = spip_ucfirst(_T('date_fmt_jour_heure', array('jour' => $s, 'heure' => $hd)));
1057
				$s = "$dtstart$s$dtabbr";
1058
			} else {
1059
				// Le <abbr...>lundi 20 fevrier de 18h00</abbr> a <abbr...>20h00</abbr>
1060
				if ($dtabbr && $dtstart && $dtend) {
1061
					$s = _T('date_fmt_jour_heure_debut_fin_abbr', array(
1062
						'jour' => spip_ucfirst($s),
1063
						'heure_debut' => $hd,
1064
						'heure_fin' => $hf,
1065
						'dtstart' => $dtstart,
1066
						'dtend' => $dtend,
1067
						'dtabbr' => $dtabbr
1068
					),
1069
						array(
1070
							'sanitize' => false
1071
						)
1072
					);
1073
				} // Le lundi 20 fevrier de 18h00 a 20h00
1074
				else {
1075
					$s = spip_ucfirst(_T('date_fmt_jour_heure_debut_fin',
1076
						array('jour' => $s, 'heure_debut' => $hd, 'heure_fin' => $hf)));
1077
				}
1078
			}
1079
		} else {
1080
			if ($dtabbr && $dtstart) {
1081
				$s = $dtstart . spip_ucfirst($s) . $dtabbr;
1082
			} else {
1083
				$s = spip_ucfirst($s);
1084
			}
1085
		}
1086
	} else {
1087
		if ((date("Y-m", $date_debut)) == date("Y-m", $date_fin)) { // meme annee et mois, jours differents
1088
			if (!$h) {
1089
				$date_debut = jour($d);
1090
			} else {
1091
				$date_debut = affdate_jourcourt($d, date("Y", $date_fin));
1092
			}
1093
			$date_fin = $affdate($f);
1094 View Code Duplication
			if ($jour) {
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...
1095
				$nomjour_debut = nom_jour($d, $abbr);
1096
				$date_debut = _T('date_fmt_jour', array('nomjour' => $nomjour_debut, 'jour' => $date_debut));
1097
				$nomjour_fin = nom_jour($f, $abbr);
1098
				$date_fin = _T('date_fmt_jour', array('nomjour' => $nomjour_fin, 'jour' => $date_fin));
1099
			}
1100 View Code Duplication
			if ($h) {
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...
1101
				$date_debut = _T('date_fmt_jour_heure', array('jour' => $date_debut, 'heure' => $hd));
1102
				$date_fin = _T('date_fmt_jour_heure', array('jour' => $date_fin, 'heure' => $hf));
1103
			}
1104
			$date_debut = $dtstart . $date_debut . $dtabbr;
1105
			$date_fin = $dtend . $date_fin . $dtabbr;
1106
1107
			$s = _T('date_fmt_periode', array('date_debut' => $date_debut, 'date_fin' => $date_fin));
1108
		} else {
1109
			$date_debut = affdate_jourcourt($d, date("Y", $date_fin));
1110
			$date_fin = $affdate($f);
1111 View Code Duplication
			if ($jour) {
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...
1112
				$nomjour_debut = nom_jour($d, $abbr);
1113
				$date_debut = _T('date_fmt_jour', array('nomjour' => $nomjour_debut, 'jour' => $date_debut));
1114
				$nomjour_fin = nom_jour($f, $abbr);
1115
				$date_fin = _T('date_fmt_jour', array('nomjour' => $nomjour_fin, 'jour' => $date_fin));
1116
			}
1117 View Code Duplication
			if ($h) {
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...
1118
				$date_debut = _T('date_fmt_jour_heure', array('jour' => $date_debut, 'heure' => $hd));
1119
				$date_fin = _T('date_fmt_jour_heure', array('jour' => $date_fin, 'heure' => $hf));
1120
			}
1121
1122
			$date_debut = $dtstart . $date_debut . $dtabbr;
1123
			$date_fin = $dtend . $date_fin . $dtabbr;
1124
			$s = _T('date_fmt_periode', array('date_debut' => $date_debut, 'date_fin' => $date_fin));
1125
1126
		}
1127
	}
1128
1129
	return $s;
1130
}
1131
1132
/**
1133
 * Adapte une date pour être insérée dans une valeur de date d'un export ICAL
1134
 *
1135
 * Retourne une date au format `Ymd\THis\Z`, tel que '20150428T163254Z'
1136
 *
1137
 * @example `DTSTAMP:[(#DATE|date_ical)]`
1138
 * @filtre
1139
 * @uses recup_heure()
1140
 * @uses recup_date()
1141
 *
1142
 * @param string $date
1143
 *     La date
1144
 * @param int $addminutes
1145
 *     Ajouter autant de minutes à la date
1146
 * @return string
1147
 *     Date au format ical
1148
 **/
1149
function date_ical($date, $addminutes = 0) {
1150
	list($heures, $minutes, $secondes) = recup_heure($date);
1151
	list($annee, $mois, $jour) = recup_date($date);
1152
1153
	return gmdate("Ymd\THis\Z", mktime($heures, $minutes + $addminutes, $secondes, $mois, $jour, $annee));
1154
}
1155
1156
1157
/**
1158
 * Retourne une date formattée au format "RFC 3339" ou "ISO 8601"
1159
 *
1160
 * @example `[(#DATE|date_iso)]` peut donner "2015-11-11T10:13:45Z"
1161
 *
1162
 * @filtre
1163
 * @link https://www.spip.net/5641
1164
 * @link https://fr.wikipedia.org/wiki/ISO_8601
1165
 * @link http://www.ietf.org/rfc/rfc3339.txt
1166
 * @link http://php.net/manual/fr/class.datetime.php
1167
 *
1168
 * @uses recup_date()
1169
 * @uses recup_heure()
1170
 *
1171
 * @param string $date_heure
1172
 *     Une écriture de date
1173
 * @return string
1174
 *     La date formatée
1175
 **/
1176 View Code Duplication
function date_iso($date_heure) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1177
	list($annee, $mois, $jour) = recup_date($date_heure);
1178
	list($heures, $minutes, $secondes) = recup_heure($date_heure);
1179
	$time = @mktime($heures, $minutes, $secondes, $mois, $jour, $annee);
1180
1181
	return gmdate('Y-m-d\TH:i:s\Z', $time);
1182
}
1183
1184
/**
1185
 * Retourne une date formattée au format "RFC 822"
1186
 *
1187
 * Utilisé pour `<pubdate>` dans certains flux RSS
1188
 *
1189
 * @example `[(#DATE|date_822)]` peut donner "Wed, 11 Nov 2015 11:13:45 +0100"
1190
 *
1191
 * @filtre
1192
 * @link https://www.spip.net/4276
1193
 * @link http://php.net/manual/fr/class.datetime.php
1194
 *
1195
 * @uses recup_date()
1196
 * @uses recup_heure()
1197
 *
1198
 * @param string $date_heure
1199
 *     Une écriture de date
1200
 * @return string
1201
 *     La date formatée
1202
 **/
1203 View Code Duplication
function date_822($date_heure) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1204
	list($annee, $mois, $jour) = recup_date($date_heure);
1205
	list($heures, $minutes, $secondes) = recup_heure($date_heure);
1206
	$time = mktime($heures, $minutes, $secondes, $mois, $jour, $annee);
1207
1208
	return date('r', $time);
1209
}
1210
1211
/**
1212
 * Pour une date commençant par `Y-m-d`, retourne `Ymd`
1213
 *
1214
 * @example `date_anneemoisjour('2015-10-11 11:27:03')` retourne `20151011`
1215
 * @see date_anneemois()
1216
 *
1217
 * @param string $d
1218
 *     Une écriture de date commençant par un format `Y-m-d` (comme date ou datetime SQL).
1219
 *     Si vide, utilise la date actuelle.
1220
 * @return string
1221
 *     Date au format `Ymd`
1222
 **/
1223 View Code Duplication
function date_anneemoisjour($d) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1224
	if (!$d) {
1225
		$d = date("Y-m-d");
1226
	}
1227
1228
	return substr($d, 0, 4) . substr($d, 5, 2) . substr($d, 8, 2);
1229
}
1230
1231
/**
1232
 * Pour une date commençant par `Y-m`, retourne `Ym`
1233
 *
1234
 * @example `date_anneemoisjour('2015-10-11 11:27:03')` retourne `201510`
1235
 * @see date_anneemoisjour()
1236
 *
1237
 * @param string $d
1238
 *     Une écriture de date commençant par un format `Y-m` (comme date ou datetime SQL).
1239
 *     Si vide, utilise la date actuelle.
1240
 * @return string
1241
 *     Date au format `Ym`
1242
 **/
1243 View Code Duplication
function date_anneemois($d) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1244
	if (!$d) {
1245
		$d = date("Y-m-d");
1246
	}
1247
1248
	return substr($d, 0, 4) . substr($d, 5, 2);
1249
}
1250
1251
/**
1252
 * Retourne le premier jour (lundi) de la même semaine au format `Ymd`
1253
 *
1254
 * @example `date_debut_semaine(2015, 11, 11)` retourne `20151109`
1255
 * @see date_fin_semaine()
1256
 *
1257
 * @param int $annee
1258
 * @param int $mois
1259
 * @param int $jour
1260
 * @return string
1261
 *     Date au lundi de la même semaine au format `Ymd`
1262
 **/
1263 View Code Duplication
function date_debut_semaine($annee, $mois, $jour) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1264
	$w_day = date("w", mktime(0, 0, 0, $mois, $jour, $annee));
1265
	if ($w_day == 0) {
1266
		$w_day = 7;
1267
	} // Gaffe: le dimanche est zero
1268
	$debut = $jour - $w_day + 1;
1269
1270
	return date("Ymd", mktime(0, 0, 0, $mois, $debut, $annee));
1271
}
1272
1273
/**
1274
 * Retourne le dernier jour (dimanche) de la même semaine au format `Ymd`
1275
 *
1276
 * @example `date_debut_semaine(2015, 11, 11)` retourne `20151115`
1277
 * @see date_fin_semaine()
1278
 *
1279
 * @param int $annee
1280
 * @param int $mois
1281
 * @param int $jour
1282
 * @return string
1283
 *     Date au dimanche de la même semaine au format `Ymd`
1284
 **/
1285 View Code Duplication
function date_fin_semaine($annee, $mois, $jour) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
1286
	$w_day = date("w", mktime(0, 0, 0, $mois, $jour, $annee));
1287
	if ($w_day == 0) {
1288
		$w_day = 7;
1289
	} // Gaffe: le dimanche est zero
1290
	$debut = $jour - $w_day + 1;
1291
1292
	return date("Ymd", mktime(0, 0, 0, $mois, $debut + 6, $annee));
1293
}
1294
1295