1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
<?php
// php/DateTimestamp.php
class Dates
{
private $date;
private $timestamp; // valeurs négatives autorisées => dates avant 1970
static public $date_format = 'EU'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US)
public function __construct($entry)
{
if(gettype($entry) === 'string') // une date est attendue
{
$this->setDate($entry);
}
elseif(gettype($entry) === 'integer')
{
$this->setTimestamp($entry);
}
}
public function setDate(string $entry)
{
$entry = $this->dashOrSlash($entry); // pour strtotime()
$splitedDate = preg_split('#\D#', $entry); // \D = tout sauf chiffre
if(self::$date_format === 'EU')
{
$tmp = $splitedDate[0];
$splitedDate[0] = $splitedDate[1];
$splitedDate[1] = $tmp;
}
if(checkdate($splitedDate[0], $splitedDate[1], $splitedDate[2]))
{
$this->date = $entry;
$this->timestamp = strtotime($entry); // date (string) -> timestamp (int)
// strtotime() devine le format en analysant la chaîne en entrée, on l'aide un peu
// avec des /, php considère que la date est américaine
// avec des - ou des ., php considère que la date est européenne
}
else
{
echo("Date incorrecte, le format de la date dans le fichier config.php est " . self::$date_format . ".\nLes choix possibles sont EU pour Europe et US pour États-Unis.");
die();
}
}
public function setTimestamp(int $entry)
{
$this->timestamp = $entry;
$this->date = $this->timestamp_to_date($entry); // timestamp (int) -> date (string)
}
public function getDate(): string
{
return($this->date);
}
public function getTimestamp(): int
{
return($this->timestamp);
}
private function dashOrSlash(string $date): string
{
if(self::$date_format === 'EU')
{
// change jj/mm/aaaa en jj-mm-aaaa
return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre
}
elseif(self::$date_format === 'US')
{
// change mm-dd.yyyy en mm/dd/yyyy
return(preg_replace('#\D#', '/', $date));
}
else
{
echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"');
die(); // brutal
}
}
private function timestamp_to_date(int $timestamp): string
{
if(self::$date_format === 'EU')
{
return(date("j-m-Y", $timestamp));
}
elseif(self::$date_format === 'US')
{
return(date("m/d/Y", $timestamp));
}
else
{
echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"');
die(); // brutal
}
}
}
|