TimeFormat.class.php
6.19 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
define('TIME_FORMAT_MINITE', '%s分钟前');
define('TIME_FORMAT_TODAY', '今天 %s');
define('TIME_FORMAT_YESTODAY', '昨天 %s');
define('TIME_FORMAT_HISTORY', '%s-%s-%s');
define('TIME_FORMAT_HISTORY_VISITOR', '%s月%s日');
define('TIME_FORMAT_CAPTION_TODAY','今天');
define('TIME_FORMAT_CAPTION_YESTODAY','昨天');
define('TIME_FORMAT_CAPTION_YEAR','年');
define('TIME_FORMAT_CAPTION_MONTH','月');
define('TIME_FORMAT_CAPTION_DAY','日');
define('TIME_FORMAT_CAPTION_HOUR','点');
define('TIME_FORMAT_CAPTION_MINITE','分');
define('TIME_FORMAT_CAPTION_SECOND','秒');
define('TIME_FORMAT_EVENT_NOYEAR', '%s月%s日 周%s %s');
define('TIME_FORMAT_EVENT_WITHYEAR', '%s年%s月%s日 周%s %s');
define('TIME_FORMAT_WHOLE', '%s年%s月%s日 %s');
define('TIME_FORMAT_EVENT_MINUTE', '%s:%s');
class Lib_Utils_TimeFormat
{
public static function timeFormat($time)
{
$now = time();
if (strpos($time, '-') !== false)
{
$time = strtotime($time);
}
if (($dur = $now - $time) < 3600)
{
$minutes = ceil($dur / 60);
if ($minutes <= 0)
{
$minutes = 1;
}
$time = sprintf(TIME_FORMAT_MINITE, $minutes);
}
else if (date('Ymd', $now) == date('Ymd', $time))
{
$time = sprintf(TIME_FORMAT_TODAY, date('H:i', $time));
}
else
{
if (date('Y') == date('Y',$time))
{
$time = sprintf(TIME_FORMAT_HISTORY_VISITOR,date('n',$time),date('j',$time)) . ' ' . date('H:i',$time);
}
else
{
$time = sprintf(TIME_FORMAT_HISTORY, date('Y',$time),date('n',$time),date('j',$time)) . ' ' . date('H:i',$time);
}
}
return $time;
}
public static function staticHtmlTimeFormat($time)
{
$now = time();
if (strpos($time, '-')!==false)
{
$time = strtotime($time);
}
$time = sprintf(TIME_FORMAT_WHOLE, date('Y',$time),date('n',$time), date('j',$time), date('H:i',$time));
return $time;
}
public static function timeFormatGroup($time)
{
$now = time();
if (strpos($time, '-') !== false)
{
$time = strtotime($time);
}
if(($dur = $now - $time) < 3600)
{
$minutes = ceil($dur / 60);
if ($minutes<=0)
{
$minutes = 1;
}
$time = sprintf(TIME_FORMAT_MINITE, $minutes);
}
else if (date('Ymd', $now) == date('Ymd', $time))
{
$time = sprintf(TIME_FORMAT_TODAY, date('H:i', $time));
}
else
{
$time = sprintf('%s-%s %s:%s', date('m',$time),date('d',$time),date('H',$time),date('i',$time));
}
return $time;
}
public static function timeFormatVisitor($time)
{
$now = time();
if(($dur = $now - $time) < 3600)
{
$minutes = ceil($dur / 60);
if ($minutes<=0)
{
$minutes = 1;
}
$time = sprintf(TIME_FORMAT_MINITE, $minutes);
}
elseif (date('Ymd', $now) == date('Ymd', $time))
{
$time = sprintf(TIME_FORMAT_TODAY, date('H:i', $time));
}
else
{
$time = sprintf(TIME_FORMAT_HISTORY_VISITOR, date('n',$time),date('j',$time));
}
return $time;
}
public static function timeFormatArr($time)
{
$retime = array();
$now = time();
if (strpos($time, '-') !== false)
{
$time = strtotime($time);
}
if(($dur = $now - $time) < 3600)
{
$minutes = ceil($dur / 60);
if ($minutes<=0)
{
$minutes = 1;
}
$retime['date'] = TIME_FORMAT_CAPTION_TODAY;
$retime['time'] = sprintf(TIME_FORMAT_MINITE, $minutes);
}
else if (date('Ymd', $now) == date('Ymd', $time))
{
$retime['date'] = TIME_FORMAT_CAPTION_TODAY;
$retime['time'] = date('H:i', $time);
}
else
{
$retime['date'] = date('n', $time).TIME_FORMAT_CAPTION_MONTH.date('j', $time).TIME_FORMAT_CAPTION_DAY;
$retime['time'] = date('H:i', $time);
}
return $retime;
}
public static function eventTimeFormat($start, $end)
{
$week = array(1=>'一',2=>'二',3=>'三',4=>'四',5=>'五',6=>'六',7=>'日');
$sweek = date('N', $start);
$eweek = date('N', $end);
if (date('Ymd', $start) == date('Ymd', $end)) //同一天
{
$time = sprintf(TIME_FORMAT_EVENT_NOYEAR, date('n', $start), date('j', $start), $week[$sweek], date('H:i', $start) . ' - ' . date('H:i', $end));
}
else if (date('Y', $start) == date('Y', $end)) //同一年不同天
{
$stime = sprintf(TIME_FORMAT_EVENT_NOYEAR, date('n', $start), date('j', $start), $week[$sweek], date('H:i', $start));
$etime = sprintf(TIME_FORMAT_EVENT_NOYEAR, date('n', $end), date('j', $end), $week[$eweek], date('H:i', $end));
$time = $stime . ' - ' . $etime;
}
else //不是同一年
{
$stime = sprintf(TIME_FORMAT_EVENT_WITHYEAR, date('Y', $start), date('n', $start), date('j', $start), $week[$sweek], date('H:i', $start));
$etime = sprintf(TIME_FORMAT_EVENT_WITHYEAR, date('Y', $end), date('n', $end), date('j', $end), $week[$eweek], date('H:i', $end));
$time = $stime . ' - ' . $etime;
}
return $time;
}
/**
* 倒计时格式化
*
* @param int $begin_time 开始时间
* @param int $end_time 结束时间
* @return array {unit:单位 , num:数值}
*/
public static function countdownFormat( $begin_time , $end_time)
{
$unit = $num = '' ;
$offset = $end_time - $begin_time ;
$max_second = 60 ;
$hour_second = 60*60 ;
$day_sencond = 24*60*60 ;
do
{
if ($offset <= 0 )
{
$unit = '秒';
$num = 0 ;
break ;
}
//秒 小于指定数则
if ($offset <= $max_second)
{
$unit = '秒';
$num = $offset ;
}
//分钟
if ($offset > $max_second && $offset < $hour_second)
{
$unit = '分钟';
$num = intval($offset/$max_second );
break ;
}
//小时
if ($offset < $day_sencond)
{
$unit = '小时';
$num = round($offset/$hour_second,1 );
break ;
}
//天
$num = round( $offset/$day_sencond,1 );
$unit = '天';
break ;
}while (false);
return array('unit' => $unit, 'num' => $num) ;
}
/**
* 分钟格式化
*
* @param int $time
* @return string
*/
public static function minuteFormat($time)
{
$minutes = sprintf('%02d', floor($time / 60));
$seconds = sprintf('%02d', ceil($time % 60));
return sprintf(TIME_FORMAT_EVENT_MINUTE, $minutes, $seconds);
}
}