summaryrefslogtreecommitdiff
path: root/src/Dates.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2026-10-17 23:47:31 +0200
committerpolo <ordipolo@gmx.fr>2026-10-17 23:47:45 +0200
commit32843e50aa981f190bdb4fbc9adbeacc06fdf775 (patch)
treea24ca45e170a69546a251f284dc1ebc578d14be8 /src/Dates.php
parent93c5a89a7a4ff7e7e3d83fd6799df96b7da4283a (diff)
downloadAppliGestionPHP-32843e50aa981f190bdb4fbc9adbeacc06fdf775.tar.gz
AppliGestionPHP-32843e50aa981f190bdb4fbc9adbeacc06fdf775.tar.bz2
AppliGestionPHP-32843e50aa981f190bdb4fbc9adbeacc06fdf775.zip
retypage date prestations en string 3, classe Dates supprimée
Diffstat (limited to 'src/Dates.php')
-rw-r--r--src/Dates.php140
1 files changed, 0 insertions, 140 deletions
diff --git a/src/Dates.php b/src/Dates.php
deleted file mode 100644
index d7ed6c7..0000000
--- a/src/Dates.php
+++ /dev/null
@@ -1,140 +0,0 @@
1<?php
2// src/DateTimestamp.php
3//
4// attention à strtotime(), php "devine" le format de la date selon qu'elle comporte des slashs / ou des tirets -
5// pour php une date avec des slashs / est américaine, mais si on fait comme ça aussi par ici
6
7class Dates
8{
9 private $date = '';
10 static public $date_format = 'euro'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US)
11
12 private $day = '';
13 private $month = '';
14 private $year = '';
15
16 private $timestamp = 0; // valeurs négatives autorisées => dates avant 1970
17
18 public function __construct($input = NULL)
19 {
20 if(gettype($input) === 'string' && $input !== '') // une date est attendue
21 {
22 $this->setDate($input);
23 }
24 elseif(gettype($input) === 'integer' && $input !== 0)
25 {
26 $this->setTimestamp($input);
27 $this->setDayMonthYear($this->date);
28 }
29 }
30
31
32 // getters
33 public function getDate(): string
34 {
35 return $this->date;
36 }
37 public function getDay(): string
38 {
39 return $this->day;
40 }
41 public function getMonth(): string
42 {
43 return $this->month;
44 }
45 public function getYear(): string
46 {
47 return $this->year;
48 }
49
50 public function getTimestamp(): int
51 {
52 return $this->timestamp;
53 }
54
55
56 // setters
57 public function setDate(string $input)
58 {
59 $input = $this->dashOrSlash($input); // formate pour strtotime()
60 $this->setDayMonthYear($input);
61
62 if(checkdate($this->month, $this->day, $this->year)) // checkdate() veut un format américain
63 {
64 $this->date = $input;
65 $this->timestamp = strtotime($input); // date (string) -> timestamp (int)
66 // strtotime() devine le format en analysant la chaîne en entrée, on l'aide un peu
67 // avec des /, php considère que la date est américaine
68 // avec des - ou des ., php considère que la date est européenne
69 }
70 else
71 {
72 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.");
73 die();
74 }
75 }
76
77 public function setTimestamp(int $input)
78 {
79 $this->timestamp = $input;
80 $this->date = $this->timestampToDate($input); // timestamp (int) -> date (string)
81 }
82
83 public function setDayMonthYear(string $input)
84 {
85 $splitedDate = preg_split('#\D#', $input); // \D = tout sauf chiffre
86
87 if(self::$date_format === 'euro')
88 {
89 $this->day = $splitedDate[0];
90 $this->month = $splitedDate[1];
91 }
92 elseif(self::$date_format === 'usa')
93 {
94 $this->day = $splitedDate[1];
95 $this->month = $splitedDate[0];
96 }
97 else
98 {
99 echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
100 die(); // brutal
101 }
102 $this->year = $splitedDate[2];
103 }
104
105 private function dashOrSlash(string $date): string
106 {
107 if(self::$date_format === 'euro')
108 {
109 // change jj/mm/aaaa en jj-mm-aaaa
110 return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre
111 }
112 elseif(self::$date_format === 'usa')
113 {
114 // change mm-dd.yyyy en mm/dd/yyyy
115 return(preg_replace('#\D#', '/', $date));
116 }
117 else
118 {
119 echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
120 die(); // brutal
121 }
122 }
123
124 private function timestampToDate(int $timestamp): string
125 {
126 if(self::$date_format === 'euro')
127 {
128 return(date("d-m-Y", $timestamp));
129 }
130 elseif(self::$date_format === 'usa')
131 {
132 return(date("m/d/Y", $timestamp));
133 }
134 else
135 {
136 echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
137 die(); // brutal
138 }
139 }
140}