diff options
Diffstat (limited to 'php/DateTimestamp.php')
| -rw-r--r-- | php/DateTimestamp.php | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/php/DateTimestamp.php b/php/DateTimestamp.php new file mode 100644 index 0000000..ca07b0a --- /dev/null +++ b/php/DateTimestamp.php | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | <?php | ||
| 2 | // php/DateTimestamp.php | ||
| 3 | |||
| 4 | class DateTimestamp | ||
| 5 | { | ||
| 6 | private $date; | ||
| 7 | static public $date_format = 'EU'; // dates européennes jj-mm-aaaa par défaut | ||
| 8 | |||
| 9 | // date jour/mois/année (string) -> timestamp (int) | ||
| 10 | private function get_timestamp(): int | ||
| 11 | { | ||
| 12 | if(self::$date_format === 'EU') | ||
| 13 | { | ||
| 14 | // change jj/mm/aaaa en jj-mm-aaaa | ||
| 15 | $this->date = preg_replace('#/#', '-', $this->date); | ||
| 16 | } | ||
| 17 | elseif(self::$date_format === 'US') | ||
| 18 | { | ||
| 19 | // change mm-dd.yyyy en mm/dd/yyyy | ||
| 20 | $this->date = preg_replace('#[-\.]#', '/', $this->date); | ||
| 21 | } | ||
| 22 | else | ||
| 23 | { | ||
| 24 | echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); | ||
| 25 | die(); // brutal | ||
| 26 | } | ||
| 27 | return(strtotime($this->date)); | ||
| 28 | // strtotime() devine le format en analysant la chaîne en entrée, on l'aide un peu | ||
| 29 | // avec des /, php considère que la date est américaine | ||
| 30 | // avec des - ou des ., php considère que la date est européenne | ||
| 31 | } | ||
| 32 | |||
| 33 | // timestamp (int) -> date jj-mm-aaaa (string) | ||
| 34 | private function get_date(): string | ||
| 35 | { | ||
| 36 | if(self::$date_format === 'EU') | ||
| 37 | { | ||
| 38 | return(date("j-m-Y", $this->date)); | ||
| 39 | } | ||
| 40 | elseif(self::$date_format === 'US') | ||
| 41 | { | ||
| 42 | return(date("m/d/Y", $this->date)); | ||
| 43 | } | ||
| 44 | else | ||
| 45 | { | ||
| 46 | echo('Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur "EU" ou "US"'); | ||
| 47 | die(); // brutal | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
