-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeHelper.class.php
More file actions
86 lines (64 loc) · 1.93 KB
/
TimeHelper.class.php
File metadata and controls
86 lines (64 loc) · 1.93 KB
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
<?PHP
/*
#LICENSE BEGIN
**********************************************************************
* OgerArch - Archaeological Database is released under the GNU General Public License (GPL) <http://www.gnu.org/licenses>
* Copyright (C) Gerhard Öttl <gerhard.oettl@ogersoft.at>
**********************************************************************
#LICENSE END
*/
/**
* Class for simple date time handling. So simple, that this functions
* do not go into OgerDateTimeBase.
* A collection of static methods.
*/
class TimeHelper {
public static $dateFormat = 'Y-m-d';
public static $timeFormat = 'H:i';
public static $dateTimeFormat = 'c';
/**
* Format a unix timestamp to a datetime. Accepts string.
* False can be used as empty datetime. Null and true are 'now'.
*/
public static function format($stamp = null, $format = null) {
if ($stamp === null || $stamp === true) {
$stamp = time();
}
elseif (is_string($stamp)) {
$stamp = trim($stamp);
// handle sql-nulldate
if (substr($stamp, 0, 10) == '0000-00-00') {
$stamp = '';
}
// convert string to timestamp
if ($stamp) {
$stamp = strtotime($stamp);
}
}
// handle empty strings and false
if (!$stamp) {
return '';
}
// convert to time and format the result
return date(($format ?: self::$dateTimeFormat), $stamp);
} // eo format datetime
/**
* Format a unix timestamp to a date.
*/
public static function formatDate($stamp = null, $format = null) {
return self::format($stamp, ($format ?: self::$dateFormat));
} // eo format date
/**
* Format a unix timestamp with ansi date format.
*/
public static function formatAnsiDate($stamp = null) {
return self::format($stamp, 'Y-m-d');
} // eo format ansi date
/**
* Format a unix timestamp to a time.
*/
public static function formatTime($stamp = null, $format = null) {
return self::format($stamp, ($format ?: self::$timeFormat));
} // eo format time
} // end of class
?>