diff options
| author | polo <ordipolo@gmx.fr> | 2022-11-29 04:43:39 +0100 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2022-11-29 04:43:39 +0100 |
| commit | 473ee4ec50a6bfdfa5c145471b077b7e3749beeb (patch) | |
| tree | 021131b50fda212f8c8698a494551b3758cdc302 /php/Model.php | |
| parent | 537599eab12656e8d1ec40b085b687714483f4fc (diff) | |
| download | AppliGestionPHP-473ee4ec50a6bfdfa5c145471b077b7e3749beeb.tar.gz AppliGestionPHP-473ee4ec50a6bfdfa5c145471b077b7e3749beeb.tar.bz2 AppliGestionPHP-473ee4ec50a6bfdfa5c145471b077b7e3749beeb.zip | |
fonctions date <-> timestamp
Diffstat (limited to 'php/Model.php')
| -rw-r--r-- | php/Model.php | 49 |
1 files changed, 48 insertions, 1 deletions
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 @@ | |||
| 4 | class Model extends Connection | 4 | class Model extends Connection |
| 5 | { | 5 | { |
| 6 | private $db; // instance de connexion | 6 | private $db; // instance de connexion |
| 7 | private $date = ''; | ||
| 8 | static public $date_format; // dates européennes jj-mm-aaaa | ||
| 9 | // pourquoi ne pas mettre les fonctions concernées dans une interface? | ||
| 7 | 10 | ||
| 8 | public function __construct() | 11 | public function __construct() |
| 9 | { | 12 | { |
| 10 | $this->db = parent::getInstance(); | 13 | $this->db = parent::getInstance(); // connexion |
| 11 | self::create_tables(); | 14 | self::create_tables(); |
| 12 | } | 15 | } |
| 13 | 16 | ||
| @@ -39,4 +42,48 @@ class Model extends Connection | |||
| 39 | 42 | ||
| 40 | // pour les dates, on stockera à priori le timestamp | 43 | // pour les dates, on stockera à priori le timestamp |
| 41 | } | 44 | } |
| 45 | |||
| 46 | |||
| 47 | // date jour/mois/année (string) -> timestamp (int) | ||
| 48 | private function get_timestamp(): int | ||
| 49 | { | ||
| 50 | if(self::$date_format == 'EU') | ||
| 51 | { | ||
| 52 | // change jj/mm/aaaa en jj-mm-aaaa | ||
| 53 | $this->date = preg_replace('#/#', '-', $this->date); | ||
| 54 | } | ||
| 55 | elseif(self::$date_format == 'US') | ||
| 56 | { | ||
| 57 | // change mm-dd.yyyy en mm/dd/yyyy | ||
| 58 | $this->date = preg_replace('#[-\.]#', '/', $this->date); | ||
| 59 | } | ||
| 60 | else | ||
| 61 | { | ||
| 62 | echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); | ||
| 63 | die(); // brutal | ||
| 64 | } | ||
| 65 | return(strtotime($this->date)); | ||
| 66 | // strtotime() devine le format en analysant la chaîne en entrée, on l'aide un peu | ||
| 67 | // avec des /, php considère que la date est américaine | ||
| 68 | // avec des - ou des ., php considère que la date est européenne | ||
| 69 | } | ||
| 70 | |||
| 71 | // timestamp (int) -> date jj-mm-aaaa (string) | ||
| 72 | private function get_date(): string | ||
| 73 | { | ||
| 74 | if(self::$date_format == 'EU') | ||
| 75 | { | ||
| 76 | return(date("j-m-Y", $this->date)); | ||
| 77 | } | ||
| 78 | elseif(self::$date_format == 'US') | ||
| 79 | { | ||
| 80 | return(date("m/d/Y", $this->date)); | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); | ||
| 85 | die(); // brutal | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 42 | } | 89 | } |
