|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Contains some tools to work with unixtimes |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Date |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\date; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class for unixtime operations |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package Date |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
class Unixtime |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Creates a DateTimeZone object with the users timezone setting |
|
33
|
|
|
* |
|
34
|
|
|
* @return \DateTimeZone |
|
35
|
|
|
**/ |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public static function userTimeZone() |
|
38
|
|
|
{ |
|
39
|
|
|
static $timezone = null; |
|
40
|
|
|
|
|
41
|
|
|
if ($timezone === null) { |
|
42
|
|
|
|
|
43
|
|
|
// @TODO: Get time zone of user |
|
44
|
|
|
$user_zone = 'Europe/Berlin'; |
|
45
|
|
|
|
|
46
|
|
|
// Test if the user has a valid timezone and use German otherwise |
|
47
|
|
|
$timezones = \DateTimeZone::listIdentifiers(); |
|
48
|
|
|
|
|
49
|
|
|
if (in_array($user_zone, $timezones)) { |
|
50
|
|
|
|
|
51
|
|
|
$timezone = new \DateTimeZone($user_zone); |
|
52
|
|
|
} else { |
|
53
|
|
|
|
|
54
|
|
|
$timezone = new \DateTimeZone('UTC'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $timezone; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Looks for the timezone difference in seconds |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $unix Unixtime |
|
65
|
|
|
* |
|
66
|
|
|
* @return int |
|
67
|
|
|
**/ |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
public static function userOffset($unix) |
|
70
|
|
|
{ |
|
71
|
|
|
$core_zone = new \DateTimeZone('UTC'); |
|
72
|
|
|
$user_zone = self::userTimeZone(); |
|
73
|
|
|
|
|
74
|
|
|
// Create date object to work with |
|
75
|
|
|
$date = \DateTime::createFromFormat('U', $unix, $core_zone); |
|
76
|
|
|
|
|
77
|
|
|
return $user_zone->getOffset($date); |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Translates a UTC+0 time to the user time |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $unix Unixtime |
|
85
|
|
|
* |
|
86
|
|
|
* @return \DateTime |
|
87
|
|
|
**/ |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
public static function userDateTime($unix) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($unix < 0) { |
|
92
|
|
|
|
|
93
|
|
|
$unix = time(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$core_zone = new \DateTimeZone('UTC'); |
|
97
|
|
|
$user_zone = self::userTimeZone(); |
|
98
|
|
|
|
|
99
|
|
|
// Create date object to work with |
|
100
|
|
|
$date = \DateTime::createFromFormat('U', $unix, $core_zone); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$date->setTimezone($user_zone); |
|
103
|
|
|
|
|
104
|
|
|
return $date; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Converts a Unixtime to String |
|
109
|
|
|
* |
|
110
|
|
|
* @param int $unix Timestamp |
|
111
|
|
|
* @param bool $dateEnabled Return Date Flag |
|
112
|
|
|
* @param bool $timeEnabled Return Time Flag |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
**/ |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
public static function string($unix, $dateEnabled=true, $timeEnabled=true) |
|
118
|
|
|
{ |
|
119
|
|
|
|
|
120
|
|
|
$date = Unixtime::userDateTime($unix); |
|
121
|
|
|
|
|
122
|
|
|
$result=""; |
|
123
|
|
|
|
|
124
|
|
|
if ($dateEnabled) { |
|
125
|
|
|
$date_format=\csphere\core\translation\fetch::key( |
|
126
|
|
|
"default", |
|
127
|
|
|
"datetime_format_date" |
|
128
|
|
|
); |
|
129
|
|
|
$result .= $date->format($date_format); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($dateEnabled && $timeEnabled) { |
|
133
|
|
|
$concat=\csphere\core\translation\fetch::key( |
|
134
|
|
|
"default", |
|
135
|
|
|
"datetime_format_concat" |
|
136
|
|
|
); |
|
137
|
|
|
$result.=" ".$concat." "; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if ($timeEnabled) { |
|
141
|
|
|
$time_format=\csphere\core\translation\fetch::key( |
|
142
|
|
|
"default", |
|
143
|
|
|
"datetime_format_time" |
|
144
|
|
|
); |
|
145
|
|
|
$time_appendix=\csphere\core\translation\fetch::key( |
|
146
|
|
|
"default", |
|
147
|
|
|
"datetime_format_appendix" |
|
148
|
|
|
); |
|
149
|
|
|
$result .=$date->format($time_format)." ".$time_appendix; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $result; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|