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 = NULL) { 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 } } }