summaryrefslogtreecommitdiff
path: root/php/DateTimestamp.php
blob: ca07b0a7813764b646db810969283d4349c0aa02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
// php/DateTimestamp.php

class DateTimestamp
{
    private $date;
    static public $date_format = 'EU'; // dates européennes jj-mm-aaaa par défaut
    
    // 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
        }
    }
}