Date.class.php
4.21 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
<?php
/**
* 时间
*
* @name Date
* @version
* @package QLib.Utils.Date
* @author peter.zyliu liuziyang@gmail.com
* @since 1.0
*/
class Util_Utils_Date {
private static $DayMap = array(
'前天',
'昨天',
'今天',
'明天',
'后天'
);
public static $WeekMap = array(
'周日',
'周一',
'周二',
'周三',
'周四',
'周五',
'周六'
);
/**
* 活动显示的时间格式
*
* @param integer $timestamp
* @return string
*/
public static function activityDisplayDate($timestamp, $needSecond = false) {
$format = date('Y') == date('Y', $timestamp) ? ($needSecond ? 'n月j日 H:i:s ' : 'n月j日 H:i ') : ($needSecond ? 'y年n月j日 H:i:s ' : 'y年n月j日 H:i');
return date($format, $timestamp) . self::$WeekMap[date('w', $timestamp)];
}
/**
* 以人性化的方式显示时间
*
* @param integer $timestamp
* @param boolean $needSecond
* @param boolean &$recent
* @return string
*/
public static function detailDisplayDate($timestamp, $needSecond = true, &$recent = false) {
$period = (int) (ceil(strtotime(date('Y-m-d', $timestamp)) / 86400) - ceil(strtotime(date('Y-m-d')) / 86400) + 2);
$recent = $period;
$format = $needSecond ? 'H:i:s' : 'H:i';
$days = $period - 2;
if (isset(self::$DayMap[$period])) {
return self::$DayMap[$period] . ' ' . date($format, $timestamp);
}
elseif ($days > 2 && $days <= 30) {
return $days . '天后 ' . date($format, $timestamp);
}
elseif ($days > 30) {
return floor($days / 30) . '个月后 ' . date($format, $timestamp);
}
elseif ($days < -2 && $days >= -30) {
return abs($days) . '天前 ' . date($format, $timestamp);
}
else if ($days < -30) {
return abs(floor($days / 30)) . '个月前 ' . date($format, $timestamp);
}
return self::activityDisplayDate($timestamp, true);
}
/**
* 时间转换成秒
*
* @param Integer $time
* @param String $format day、Hour、Minute、Second
* @return Integer
*/
public static function timeCommuteToSecond($timestamp, $format = "hour") {
$toTime = 0;
switch ($format) {
case "day" :
$toTime = $timestamp * 24 * 60 * 60;
break;
case "hour" :
$toTime = $timestamp * 60 * 60;
break;
case "minute" :
$toTime = $timestamp * 60;
break;
case "second" :
$toTime = $timestamp;
break;
default :
$toTime = $timestamp;
}
return $toTime;
}
/**
* 秒转时间转换
* @param Integer $time
* @return String
*
*/
public static function timeToDay($time) {
$timeStr = "";
switch ($time) {
case $time > 24 * 60 * 60 :
$timeStr = floor($time / (60 * 60 * 24)) . "天";
break;
case $time > 60 * 60 :
$timeStr = floor($time / (60 * 60)) . "小时";
break;
case $time > 60 :
$timeStr = floor($time / 60) . "分钟";
break;
case $time < 60 :
$timeStr = floor($time) . "秒";
break;
default :
$timeStr = floor($time) . "秒";
}
return $timeStr;
}
/**
* 一个月中有多少天
*
* @param integer $year
* @param integer $month
*/
public static function daysCountInMonth($year, $month) {
return (int) date('d', strtotime('-1 day', strtotime('+1 month', mktime(0, 0, 0, $month, 1, $year))));
}
public static function formatTimeToString($time) {
return date('Y年-m月-d日 H点i分', strtotime($time));
}
/**
* 时间差值,返回数组
*
* @param integer $d1
* @param integer $d2
* @return array
*/
public static function timeBetweenArray($d1, $d2) {
$d = abs($d1 - $d2);
$d = ($day = floor($d / 3600 / 24)) ? $d - $day * 3600 * 24 : $d;
$d = ($hour = floor($d / 3600)) ? $d - $hour * 3600 : $d;
$d = ($minute = floor($d / 60)) ? $d - $minute * 60 : $d;
return array(
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $d
);
}
/**
* 时间差值,返回字符串
*
* @param integer $d1
* @param integer $d2
* @return string
*/
public static function timeBetweenString($d1, $d2, $needSeconds = true) {
$d = self::timeBetweenArray($d1, $d2);
$ret = $d['day'] ? $d['day'] . '天' : '';
$ret .= $d['hour'] ? $d['hour'] . '小时' : '';
$ret .= $d['minute'] ? $d['minute'] . '分' : '';
if ($needSeconds) {
$ret .= $d['second'] ? $d['second'] . '秒' : '';
}
return $ret;
}
}