summaryrefslogtreecommitdiff
path: root/src/Dates.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2022-12-28 05:19:55 +0100
committerpolo <ordipolo@gmx.fr>2022-12-28 05:19:55 +0100
commit1894fc377e6b938ea34df9980567a1634ec6ef48 (patch)
tree812db64208797ecbdabbc9673a5247fbc18ebf8b /src/Dates.php
parent7d564efbccc4b361d2fa2db2902fb35882304aae (diff)
downloadAppliGestionPHP-1894fc377e6b938ea34df9980567a1634ec6ef48.zip
réorganisation + bientôt finie la section 1
Diffstat (limited to 'src/Dates.php')
-rw-r--r--src/Dates.php78
1 files changed, 50 insertions, 28 deletions
diff --git a/src/Dates.php b/src/Dates.php
index de71b08..36b19a8 100644
--- a/src/Dates.php
+++ b/src/Dates.php
@@ -3,56 +3,66 @@
3 3
4class Dates 4class Dates
5{ 5{
6 private $date; 6 private $date = '';
7 private $timestamp; // valeurs négatives autorisées => dates avant 1970 7 static public $date_format = 'euro'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US)
8 static public $date_format = 'EU'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US)
9 8
9 private $day = '';
10 private $month = '';
11 private $year = '';
10 12
11 public function __construct($entry = NULL) 13 private $timestamp = 0; // valeurs négatives autorisées => dates avant 1970
14
15 public function __construct($input = NULL)
12 { 16 {
13 if(gettype($entry) === 'string') // une date est attendue 17 if(gettype($input) === 'string' && $input !== '') // une date est attendue
14 { 18 {
15 $this->setDate($entry); 19 $this->setDate($input);
16 } 20 }
17 elseif(gettype($entry) === 'integer') 21 elseif(gettype($input) === 'integer' && $input !== 0)
18 { 22 {
19 $this->setTimestamp($entry); 23 $this->setTimestamp($input);
20 } 24 }
21 } 25 }
22 26
23 27
24 public function setDate(string $entry) 28 public function setDate(string $input)
25 { 29 {
26 $entry = $this->dashOrSlash($entry); // pour strtotime() 30 $input = $this->dashOrSlash($input); // pour strtotime()
27 31
28 $splitedDate = preg_split('#\D#', $entry); // \D = tout sauf chiffre 32 $splitedDate = preg_split('#\D#', $input); // \D = tout sauf chiffre
33 $this->year = $splitedDate[2];
29 34
30 if(self::$date_format === 'EU') 35 if(self::$date_format === 'euro')
31 { 36 {
32 $tmp = $splitedDate[0]; 37 $this->day = $splitedDate[1];
33 $splitedDate[0] = $splitedDate[1]; 38 $this->month = $splitedDate[0];
34 $splitedDate[1] = $tmp; 39 }
40 else
41 {
42 $this->day = $splitedDate[0];
43 $this->month = $splitedDate[1];
35 } 44 }
36 45
37 if(checkdate($splitedDate[0], $splitedDate[1], $splitedDate[2])) 46 //~ if(checkdate($splitedDate[0], $splitedDate[1], $splitedDate[2]))
47 if(checkdate($this->day, $this->month, $this->year))
38 { 48 {
39 $this->date = $entry; 49 $this->date = $input;
40 $this->timestamp = strtotime($entry); // date (string) -> timestamp (int) 50 $this->timestamp = strtotime($input); // date (string) -> timestamp (int)
41 // strtotime() devine le format en analysant la chaîne en entrée, on l'aide un peu 51 // 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 52 // 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 53 // avec des - ou des ., php considère que la date est européenne
44 } 54 }
45 else 55 else
46 { 56 {
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."); 57 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.");
48 die(); 58 die();
49 } 59 }
50 } 60 }
51 61
52 public function setTimestamp(int $entry) 62 public function setTimestamp(int $input)
53 { 63 {
54 $this->timestamp = $entry; 64 $this->timestamp = $input;
55 $this->date = $this->timestamp_to_date($entry); // timestamp (int) -> date (string) 65 $this->date = $this->timestamp_to_date($input); // timestamp (int) -> date (string)
56 } 66 }
57 67
58 68
@@ -60,6 +70,18 @@ class Dates
60 { 70 {
61 return($this->date); 71 return($this->date);
62 } 72 }
73 public function getDay(): string
74 {
75 return($this->day);
76 }
77 public function getMonth(): string
78 {
79 return($this->month);
80 }
81 public function getYear(): string
82 {
83 return($this->year);
84 }
63 85
64 public function getTimestamp(): int 86 public function getTimestamp(): int
65 { 87 {
@@ -69,36 +91,36 @@ class Dates
69 91
70 private function dashOrSlash(string $date): string 92 private function dashOrSlash(string $date): string
71 { 93 {
72 if(self::$date_format === 'EU') 94 if(self::$date_format === 'euro')
73 { 95 {
74 // change jj/mm/aaaa en jj-mm-aaaa 96 // change jj/mm/aaaa en jj-mm-aaaa
75 return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre 97 return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre
76 } 98 }
77 elseif(self::$date_format === 'US') 99 elseif(self::$date_format === 'usa')
78 { 100 {
79 // change mm-dd.yyyy en mm/dd/yyyy 101 // change mm-dd.yyyy en mm/dd/yyyy
80 return(preg_replace('#\D#', '/', $date)); 102 return(preg_replace('#\D#', '/', $date));
81 } 103 }
82 else 104 else
83 { 105 {
84 echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); 106 echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
85 die(); // brutal 107 die(); // brutal
86 } 108 }
87 } 109 }
88 110
89 private function timestamp_to_date(int $timestamp): string 111 private function timestamp_to_date(int $timestamp): string
90 { 112 {
91 if(self::$date_format === 'EU') 113 if(self::$date_format === 'euro')
92 { 114 {
93 return(date("j-m-Y", $timestamp)); 115 return(date("j-m-Y", $timestamp));
94 } 116 }
95 elseif(self::$date_format === 'US') 117 elseif(self::$date_format === 'usa')
96 { 118 {
97 return(date("m/d/Y", $timestamp)); 119 return(date("m/d/Y", $timestamp));
98 } 120 }
99 else 121 else
100 { 122 {
101 echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); 123 echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
102 die(); // brutal 124 die(); // brutal
103 } 125 }
104 } 126 }