DateUtils.php 3.94 KB
Newer Older
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
<?php
//    Pastèque API
//
//    Copyright (C) 
//			2012 Scil (http://scil.coop)
//			2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
//    This file is part of Pastèque.
//
//    Pastèque is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Pastèque is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Pastèque.  If not, see <http://www.gnu.org/licenses/>.

namespace Pasteque\Server\System;

/** Date conversion utilities. */
class DateUtils
{

Karamel's avatar
Karamel committed
29
30
    /**
     * Check if two dates are equals in seconds.
31
     * @param mixed $a Something representing a date (passed to readDate).
Karamel's avatar
Karamel committed
32
     * @param mixed $b Something represinting a date (passed to readDate).
33
     * @return True if the dates are equals. False if one is not a date
Karamel's avatar
Karamel committed
34
35
     * or if they are different. Null and null are equals.
     */
36
37
38
39
40
41
42
43
44
45
46
    public static function equals($a, $b) {
        $dateA = static::readDate($a);
        $dateB = static::readDate($b);
        if ($dateA === false || $dateB === false) {
            return false;
        }
        if ($dateA === null && $dateB === null) { return true; }
        if ($dateA === null || $dateB === null) { return false; }
        return $dateA->getTimestamp() == $dateB->getTimestamp();
    }

Karamel's avatar
Karamel committed
47
48
49
50
    /**
     * Convert a DateTime to timestamp in seconds.
     * @param \DateTime|null $date The date to convert.
     * @return int|null Null if $date is null or invalid,
51
52
53
54
55
56
57
58
59
60
     * timestamp if already a timestamp or a DateTime. */
    public static function toTimestamp($date) {
        if ($date === null) { return null; }
        if ($date instanceof \DateTime) {
            return $date->getTimestamp();
        }
        if (is_int($date)) { return $date; }
        return null;
    }

Karamel's avatar
Karamel committed
61
62
    /**
     * Convert a timestamp in second to a DateTime.
63
64
     * Use DateUtils::readDate instead not to bother with the input format.
     * @param int $timestamp The timestamp.
Karamel's avatar
Karamel committed
65
     * @return DateTime|bool DateTime on success, false if not a timestamp. */
66
67
68
69
70
71
72
73
74
    public static function readTimestamp($timestamp) {
        if (!is_int($timestamp)) {
            return false;
        }
        $date = new \DateTime();
        $date->setTimestamp($timestamp);
        return $date;
    }

Karamel's avatar
Karamel committed
75
76
    /**
     * Convert a date string to a DateTime.
77
78
79
     * Use DateUtils::readDate instead not to bother with the input format.
     * @param string $string The date. Valid format are YYYY-MM-DD
     * and YYYY-MM-DD HH:mm.
Karamel's avatar
Karamel committed
80
81
82
     * @return DateTime|bool DateTime on success, false if not a valid
     * date string.
     */
83
84
85
86
87
88
89
90
    public static function readString($string) {
        $date = \DateTime::createFromFormat('!Y-m-d', $string);
        if ($date === false) {
            $date = \DateTime::createFromFormat('Y-m-d h:i', $string);
        }
        return $date;
    }

Karamel's avatar
Karamel committed
91
92
93
94
95
    /**
     * Automatically detect date format and get a DateTime.
     * @param int|string|\DateTime $input Something representing a date.
     * @return \DateTime|bool The date as a \DateTime, false if invalid.
     */
96
97
    public static function readDate($input) {
        if ($input === null) { return null; }
98
99
100
101
102
103
        if (is_numeric($input)) {
            if (is_int($input)) {
                return static::readTimestamp($input);
            } else {
                return static::readTimestamp(intval($input));
            }
104
105
106
107
        }
        if ($input instanceof \DateTime) {
            return $input;
        }
108
109
110
        if (is_string($input)) {
            return static::readString($input);
        }
111
112
113
        return false;
    }
}