summaryrefslogtreecommitdiff
path: root/src/Dates.php
blob: d7ed6c7308fe265c5645d5236fb3558185b6de14 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
// src/DateTimestamp.php
//
// attention à strtotime(), php "devine" le format de la date selon qu'elle comporte des slashs / ou des tirets -
// pour php une date avec des slashs / est américaine, mais si on fait comme ça aussi par ici

class Dates
{
    private $date = '';
    static public $date_format = 'euro'; // dates européennes jj-mm-aaaa (EU) ou américaines mm/dd/yyyy (US)
    
    private $day = '';
    private $month = '';
    private $year = '';
    
    private $timestamp = 0; // valeurs négatives autorisées => dates avant 1970
    
    public function __construct($input = NULL)
    {
        if(gettype($input) === 'string' && $input !== '') // une date est attendue
        {
            $this->setDate($input);
        }
        elseif(gettype($input) === 'integer' && $input !== 0)
        {
            $this->setTimestamp($input);
            $this->setDayMonthYear($this->date);
        }
    }
    
    
    // getters
    public function getDate(): string
    {
        return $this->date;
    }
    public function getDay(): string
    {
        return $this->day;
    }
    public function getMonth(): string
    {
        return $this->month;
    }
    public function getYear(): string
    {
        return $this->year;
    }
    
    public function getTimestamp(): int
    {
        return $this->timestamp;
    }
    
    
    // setters
    public function setDate(string $input)
    {
        $input = $this->dashOrSlash($input); // formate pour strtotime()
        $this->setDayMonthYear($input);
        
        if(checkdate($this->month, $this->day, $this->year)) // checkdate() veut un format américain
        {
            $this->date = $input;
            $this->timestamp = strtotime($input); // 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 'euro' pour Europe et 'usa' pour États-Unis.");
            die();
        }
    }
    
    public function setTimestamp(int $input)
    {
        $this->timestamp = $input;
        $this->date = $this->timestampToDate($input); // timestamp (int) -> date (string)
    }
    
    public function setDayMonthYear(string $input)
    {
        $splitedDate = preg_split('#\D#', $input); // \D = tout sauf chiffre
        
        if(self::$date_format === 'euro')
        {
            $this->day = $splitedDate[0];
            $this->month = $splitedDate[1];
        }
        elseif(self::$date_format === 'usa')
        {
            $this->day = $splitedDate[1];
            $this->month = $splitedDate[0];
        }
        else
        {
            echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
            die(); // brutal
        }
        $this->year = $splitedDate[2];
    }
    
    private function dashOrSlash(string $date): string
    {
        if(self::$date_format === 'euro')
        {
            // change jj/mm/aaaa en jj-mm-aaaa
            return(preg_replace('#\D#', '-', $date)); // \D = tout sauf chiffre
        }
        elseif(self::$date_format === 'usa')
        {
            // 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 'euro' ou 'usa'");
            die(); // brutal
        }
    }
    
    private function timestampToDate(int $timestamp): string
    {
        if(self::$date_format === 'euro')
        {
            return(date("d-m-Y", $timestamp));
        }
        elseif(self::$date_format === 'usa')
        {
            return(date("m/d/Y", $timestamp));
        }
        else
        {
            echo("Le fichier config.php comporte une erreur. La variable $date_format doit avoir pour valeur 'euro' ou 'usa'");
            die(); // brutal
        }
    }
}