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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
<?php
// php/DateTimestamp.php
class Dates
{
private $date = '';
static public $date_format = 'euro'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US)
private $day = '';
private $month = '';
private $year = '';
private $timestamp = 0; // valeurs négatives autorisées => dates avant 1970
public function __construct($input = NULL)
{
if(gettype($input) === 'string' && $input !== '') // une date est attendue
{
$this->setDate($input);
}
elseif(gettype($input) === 'integer' && $input !== 0)
{
$this->setTimestamp($input);
}
}
public function setDate(string $input)
{
$input = $this->dashOrSlash($input); // pour strtotime()
$splitedDate = preg_split('#\D#', $input); // \D = tout sauf chiffre
$this->year = $splitedDate[2];
if(self::$date_format === 'euro')
{
$this->day = $splitedDate[1];
$this->month = $splitedDate[0];
}
else
{
$this->day = $splitedDate[0];
$this->month = $splitedDate[1];
}
//~ if(checkdate($splitedDate[0], $splitedDate[1], $splitedDate[2]))
if(checkdate($this->day, $this->month, $this->year))
{
$this->date = $input;
$this->timestamp = strtotime($input); // 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 'euro' pour Europe et 'usa' pour États-Unis.");
die();
}
}
public function setTimestamp(int $input)
{
$this->timestamp = $input;
$this->date = $this->timestamp_to_date($input); // timestamp (int) -> date (string)
}
public function getDate(): string
{
return($this->date);
}
public function getDay(): string
{
return($this->day);
}
public function getMonth(): string
{
return($this->month);
}
public function getYear(): string
{
return($this->year);
}
public function getTimestamp(): int
{
return($this->timestamp);
}
private function dashOrSlash(string $date): string
{
if(self::$date_format === 'euro')
{
// change jj/mm/aaaa en jj-mm-aaaa
return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre
}
elseif(self::$date_format === 'usa')
{
// 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 'euro' ou 'usa'");
die(); // brutal
}
}
private function timestamp_to_date(int $timestamp): string
{
if(self::$date_format === 'euro')
{
return(date("j-m-Y", $timestamp));
}
elseif(self::$date_format === 'usa')
{
return(date("m/d/Y", $timestamp));
}
else
{
echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
die(); // brutal
}
}
}
|