diff options
author | polo <ordipolo@gmx.fr> | 2022-12-14 12:55:46 +0100 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2022-12-14 12:55:46 +0100 |
commit | ff14091476a35de16a9ea3208501040cfae93a06 (patch) | |
tree | 89ebd00ad68fbb97aac71ef8a2972748f82dee0d /src/Dates.php | |
parent | a45a3e0345890b9df3c5fa7c82966a64491eca02 (diff) | |
download | AppliGestionPHP-ff14091476a35de16a9ea3208501040cfae93a06.zip |
MODEL + reorganisation
Diffstat (limited to 'src/Dates.php')
-rw-r--r-- | src/Dates.php | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/Dates.php b/src/Dates.php new file mode 100644 index 0000000..de71b08 --- /dev/null +++ b/src/Dates.php | |||
@@ -0,0 +1,105 @@ | |||
1 | <?php | ||
2 | // php/DateTimestamp.php | ||
3 | |||
4 | class Dates | ||
5 | { | ||
6 | private $date; | ||
7 | private $timestamp; // valeurs négatives autorisées => dates avant 1970 | ||
8 | static public $date_format = 'EU'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US) | ||
9 | |||
10 | |||
11 | public function __construct($entry = NULL) | ||
12 | { | ||
13 | if(gettype($entry) === 'string') // une date est attendue | ||
14 | { | ||
15 | $this->setDate($entry); | ||
16 | } | ||
17 | elseif(gettype($entry) === 'integer') | ||
18 | { | ||
19 | $this->setTimestamp($entry); | ||
20 | } | ||
21 | } | ||
22 | |||
23 | |||
24 | public function setDate(string $entry) | ||
25 | { | ||
26 | $entry = $this->dashOrSlash($entry); // pour strtotime() | ||
27 | |||
28 | $splitedDate = preg_split('#\D#', $entry); // \D = tout sauf chiffre | ||
29 | |||
30 | if(self::$date_format === 'EU') | ||
31 | { | ||
32 | $tmp = $splitedDate[0]; | ||
33 | $splitedDate[0] = $splitedDate[1]; | ||
34 | $splitedDate[1] = $tmp; | ||
35 | } | ||
36 | |||
37 | if(checkdate($splitedDate[0], $splitedDate[1], $splitedDate[2])) | ||
38 | { | ||
39 | $this->date = $entry; | ||
40 | $this->timestamp = strtotime($entry); // date (string) -> timestamp (int) | ||
41 | // strtotime() devine le format en analysant la chaîne en entrée, on l'aide un peu | ||
42 | // avec des /, php considère que la date est américaine | ||
43 | // avec des - ou des ., php considère que la date est européenne | ||
44 | } | ||
45 | else | ||
46 | { | ||
47 | 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."); | ||
48 | die(); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | public function setTimestamp(int $entry) | ||
53 | { | ||
54 | $this->timestamp = $entry; | ||
55 | $this->date = $this->timestamp_to_date($entry); // timestamp (int) -> date (string) | ||
56 | } | ||
57 | |||
58 | |||
59 | public function getDate(): string | ||
60 | { | ||
61 | return($this->date); | ||
62 | } | ||
63 | |||
64 | public function getTimestamp(): int | ||
65 | { | ||
66 | return($this->timestamp); | ||
67 | } | ||
68 | |||
69 | |||
70 | private function dashOrSlash(string $date): string | ||
71 | { | ||
72 | if(self::$date_format === 'EU') | ||
73 | { | ||
74 | // change jj/mm/aaaa en jj-mm-aaaa | ||
75 | return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre | ||
76 | } | ||
77 | elseif(self::$date_format === 'US') | ||
78 | { | ||
79 | // change mm-dd.yyyy en mm/dd/yyyy | ||
80 | return(preg_replace('#\D#', '/', $date)); | ||
81 | } | ||
82 | else | ||
83 | { | ||
84 | echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); | ||
85 | die(); // brutal | ||
86 | } | ||
87 | } | ||
88 | |||
89 | private function timestamp_to_date(int $timestamp): string | ||
90 | { | ||
91 | if(self::$date_format === 'EU') | ||
92 | { | ||
93 | return(date("j-m-Y", $timestamp)); | ||
94 | } | ||
95 | elseif(self::$date_format === 'US') | ||
96 | { | ||
97 | return(date("m/d/Y", $timestamp)); | ||
98 | } | ||
99 | else | ||
100 | { | ||
101 | echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); | ||
102 | die(); // brutal | ||
103 | } | ||
104 | } | ||
105 | } | ||