From 1894fc377e6b938ea34df9980567a1634ec6ef48 Mon Sep 17 00:00:00 2001 From: polo Date: Wed, 28 Dec 2022 05:19:55 +0100 Subject: =?UTF-8?q?r=C3=A9organisation=20+=20bient=C3=B4t=20finie=20la=20s?= =?UTF-8?q?ection=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Dates.php | 78 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 28 deletions(-) (limited to 'src/Dates.php') 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 @@ 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) + 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 = ''; - public function __construct($entry = NULL) + private $timestamp = 0; // valeurs négatives autorisées => dates avant 1970 + + public function __construct($input = NULL) { - if(gettype($entry) === 'string') // une date est attendue + if(gettype($input) === 'string' && $input !== '') // une date est attendue { - $this->setDate($entry); + $this->setDate($input); } - elseif(gettype($entry) === 'integer') + elseif(gettype($input) === 'integer' && $input !== 0) { - $this->setTimestamp($entry); + $this->setTimestamp($input); } } - public function setDate(string $entry) + public function setDate(string $input) { - $entry = $this->dashOrSlash($entry); // pour strtotime() + $input = $this->dashOrSlash($input); // pour strtotime() - $splitedDate = preg_split('#\D#', $entry); // \D = tout sauf chiffre + $splitedDate = preg_split('#\D#', $input); // \D = tout sauf chiffre + $this->year = $splitedDate[2]; - if(self::$date_format === 'EU') + if(self::$date_format === 'euro') { - $tmp = $splitedDate[0]; - $splitedDate[0] = $splitedDate[1]; - $splitedDate[1] = $tmp; + $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($splitedDate[0], $splitedDate[1], $splitedDate[2])) + if(checkdate($this->day, $this->month, $this->year)) { - $this->date = $entry; - $this->timestamp = strtotime($entry); // date (string) -> timestamp (int) + $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 EU pour Europe et US pour États-Unis."); + 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 $entry) + public function setTimestamp(int $input) { - $this->timestamp = $entry; - $this->date = $this->timestamp_to_date($entry); // timestamp (int) -> date (string) + $this->timestamp = $input; + $this->date = $this->timestamp_to_date($input); // timestamp (int) -> date (string) } @@ -60,6 +70,18 @@ class Dates { 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 { @@ -69,36 +91,36 @@ class Dates private function dashOrSlash(string $date): string { - if(self::$date_format === 'EU') + 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 === 'US') + 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 "EU" ou "US"'); + 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 === 'EU') + if(self::$date_format === 'euro') { return(date("j-m-Y", $timestamp)); } - elseif(self::$date_format === 'US') + 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 "EU" ou "US"'); + echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'"); die(); // brutal } } -- cgit v1.2.3