From 473ee4ec50a6bfdfa5c145471b077b7e3749beeb Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 29 Nov 2022 04:43:39 +0100 Subject: fonctions date <-> timestamp --- php/Model.php | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'php/Model.php') diff --git a/php/Model.php b/php/Model.php index 9d0f80f..f3f1fe7 100644 --- a/php/Model.php +++ b/php/Model.php @@ -4,10 +4,13 @@ class Model extends Connection { private $db; // instance de connexion + private $date = ''; + static public $date_format; // dates européennes jj-mm-aaaa + // pourquoi ne pas mettre les fonctions concernées dans une interface? public function __construct() { - $this->db = parent::getInstance(); + $this->db = parent::getInstance(); // connexion self::create_tables(); } @@ -39,4 +42,48 @@ class Model extends Connection // pour les dates, on stockera à priori le timestamp } + + + // date jour/mois/année (string) -> timestamp (int) + private function get_timestamp(): int + { + if(self::$date_format == 'EU') + { + // change jj/mm/aaaa en jj-mm-aaaa + $this->date = preg_replace('#/#', '-', $this->date); + } + elseif(self::$date_format == 'US') + { + // change mm-dd.yyyy en mm/dd/yyyy + $this->date = preg_replace('#[-\.]#', '/', $this->date); + } + else + { + echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); + die(); // brutal + } + return(strtotime($this->date)); + // 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 + } + + // timestamp (int) -> date jj-mm-aaaa (string) + private function get_date(): string + { + if(self::$date_format == 'EU') + { + return(date("j-m-Y", $this->date)); + } + elseif(self::$date_format == 'US') + { + return(date("m/d/Y", $this->date)); + } + 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