dates avant 1970 public function __construct($input = NULL) { if(gettype($input) === 'string' && $input !== '') // une date est attendue { $this->setDate($input); } elseif(gettype($input) === 'integer' && $input !== 0) { $this->setTimestamp($input); $this->setDayMonthYear($this->date); } } // getters public function getDate(): string { 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 { return $this->timestamp; } // setters public function setDate(string $input) { $input = $this->dashOrSlash($input); // formate pour strtotime() $this->setDayMonthYear($input); if(checkdate($this->month, $this->day, $this->year)) // checkdate() veut un format américain { $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 'euro' pour Europe et 'usa' pour États-Unis."); die(); } } public function setTimestamp(int $input) { $this->timestamp = $input; $this->date = $this->timestampToDate($input); // timestamp (int) -> date (string) } public function setDayMonthYear(string $input) { $splitedDate = preg_split('#\D#', $input); // \D = tout sauf chiffre if(self::$date_format === 'euro') { $this->day = $splitedDate[0]; $this->month = $splitedDate[1]; } elseif(self::$date_format === 'usa') { $this->day = $splitedDate[1]; $this->month = $splitedDate[0]; } else { echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'"); die(); // brutal } $this->year = $splitedDate[2]; } private function dashOrSlash(string $date): string { 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 === '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 'euro' ou 'usa'"); die(); // brutal } } private function timestampToDate(int $timestamp): string { if(self::$date_format === 'euro') { return(date("d-m-Y", $timestamp)); } 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 'euro' ou 'usa'"); die(); // brutal } } }