From eda2e96c17d8d22cfc2615698efa5c757dbfb0a5 Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 5 Dec 2022 04:18:15 +0100 Subject: class Dates + templates latex + bugs --- php/Dates.php | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 php/Dates.php (limited to 'php/Dates.php') diff --git a/php/Dates.php b/php/Dates.php new file mode 100644 index 0000000..264166f --- /dev/null +++ b/php/Dates.php @@ -0,0 +1,105 @@ + dates avant 1970 + static public $date_format = 'EU'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US) + + + public function __construct($entry) + { + if(gettype($entry) === 'string') // une date est attendue + { + $this->setDate($entry); + } + elseif(gettype($entry) === 'integer') + { + $this->setTimestamp($entry); + } + } + + + public function setDate(string $entry) + { + $entry = $this->dashOrSlash($entry); // pour strtotime() + + $splitedDate = preg_split('#\D#', $entry); // \D = tout sauf chiffre + + if(self::$date_format === 'EU') + { + $tmp = $splitedDate[0]; + $splitedDate[0] = $splitedDate[1]; + $splitedDate[1] = $tmp; + } + + if(checkdate($splitedDate[0], $splitedDate[1], $splitedDate[2])) + { + $this->date = $entry; + $this->timestamp = strtotime($entry); // 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."); + die(); + } + } + + public function setTimestamp(int $entry) + { + $this->timestamp = $entry; + $this->date = $this->timestamp_to_date($entry); // timestamp (int) -> date (string) + } + + + public function getDate(): string + { + return($this->date); + } + + public function getTimestamp(): int + { + return($this->timestamp); + } + + + private function dashOrSlash(string $date): string + { + if(self::$date_format === 'EU') + { + // change jj/mm/aaaa en jj-mm-aaaa + return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre + } + elseif(self::$date_format === 'US') + { + // 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"'); + die(); // brutal + } + } + + private function timestamp_to_date(int $timestamp): string + { + if(self::$date_format === 'EU') + { + return(date("j-m-Y", $timestamp)); + } + elseif(self::$date_format === 'US') + { + 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"'); + die(); // brutal + } + } +} -- cgit v1.2.3