系统的友好性在开发过程中是非常重要的,一个系统非常友好不光可以带给用户非常棒的使用体验,而且还可以使系统有这更长的寿命。今天这篇博文主要来展示一个我们如何让日期时间更加友好的显示。
下面就是使时间日期友好显示的代码:核心代码
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
|
<?php /** * ======================================= * Created by Zhihua_W. * Author: Zhihua_W * Date: 2016/11/23 0001 * Time: 下午 5:45 * Project: PHP开发小技巧 * Power: 实现日期时间友好显示 * ======================================= */ /** * 日期时间友好显示 * @param $time * @return bool|string */ function friend_date( $time ) { if (! $time ) { return false; } $fdate = '' ; $d = time() - intval ( $time ); $ld = $time - mktime (0, 0, 0, 0, 0, date ( 'Y' )); //得出年 $md = $time - mktime (0, 0, 0, date ( 'm' ), 0, date ( 'Y' )); //得出月 $byd = $time - mktime (0, 0, 0, date ( 'm' ), date ( 'd' ) - 2, date ( 'Y' )); //前天 $yd = $time - mktime (0, 0, 0, date ( 'm' ), date ( 'd' ) - 1, date ( 'Y' )); //昨天 $dd = $time - mktime (0, 0, 0, date ( 'm' ), date ( 'd' ), date ( 'Y' )); //今天 $td = $time - mktime (0, 0, 0, date ( 'm' ), date ( 'd' ) + 1, date ( 'Y' )); //明天 $atd = $time - mktime (0, 0, 0, date ( 'm' ), date ( 'd' ) + 2, date ( 'Y' )); //后天 if ( $d == 0) { $fdate = '刚刚' ; } else { switch ( $d ) { case $d < $atd : $fdate = date ( 'Y年m月d日' , $time ); break ; case $d < $td : $fdate = '后天' . date ( 'H:i' , $time ); break ; case $d < 0: $fdate = '明天' . date ( 'H:i' , $time ); break ; case $d < 60: $fdate = $d . '秒前' ; break ; case $d < 3600: $fdate = floor ( $d / 60) . '分钟前' ; break ; case $d < $dd : $fdate = floor ( $d / 3600) . '小时前' ; break ; case $d < $yd : $fdate = '昨天' . date ( 'H:i' , $time ); break ; case $d < $byd : $fdate = '前天' . date ( 'H:i' , $time ); break ; case $d < $md : $fdate = date ( 'm月d日 H:i' , $time ); break ; case $d < $ld : $fdate = date ( 'm月d日' , $time ); break ; default : $fdate = date ( 'Y年m月d日' , $time ); break ; } } return $fdate ; } $time = 1442345132; echo friend_date( $time ); |
原文链接:https://blog.csdn.net/Zhihua_W/article/details/53308459