简单好用PHP日期时间处理函数,返回类似【6小时8分钟前】的格式
16-12-20 15:44
字数 2286
阅读 4335
已编辑
如果你需要显示一篇的文章的发表时间,下面的函数可能会帮到你。
/**
* Created by PhpStorm.
* @author: saonian
* @date: 2016/12/20
* @time: 13:34
*/
$now_time = date('Y-m-d H:i:s');
$time1 = '2016-12-20 14:50:50';
$time2 = '2016-12-20 09:30:12';
$time3 = '2016-12-15 09:30:12';
$time4 = '2016-12-10 09:30:12';
echo "Now: ".date('Y-m-d H:i:s');//Now: 2016-12-20 15:38:30
var_dump(getTimeDiff($now_time));//刚刚
var_dump(getTimeDiff($time1));//47 分钟 40 秒前
var_dump(getTimeDiff($time2));//6 小时 8 分钟前
var_dump(getTimeDiff($time3));//5 天 6 小时前
var_dump(getTimeDiff($time4));//12/10 09:30
function getTimeDiff($time,$lang='cn'){
$res_string = $time;
if ($lang == 'cn'){
$short = '刚刚';$ago = '前';$second = '秒';$minute = '分钟';$hour = '小时';$day = '天';
}else{
$short = 'one minute ago';$ago = 'ago';$second = 'sec';$minute = 'min';$hour = 'hour';$day = 'day';
}
if(!empty($time) && check_DT($time)){
$time_diff = abs(intval(time() - strtotime($time)));
if($time_diff < 1){
$res_string = $short;
}elseif ($time_diff < 60){//1分钟内
$res_string = $time_diff . ' ' . $second . $ago;
}elseif ($time_diff < 3600){//1小时内
$min_part = floor($time_diff / 60);
$min_part_s = $time_diff % 60;
if(!$min_part_s){
$res_string = $min_part . ' ' . $minute . $ago;
}else{
$res_string = $min_part . ' ' . $minute . ' ' . $min_part_s . ' ' . $second.$ago;
}
}elseif ($time_diff < 86400){//1天内
$hour_part = floor($time_diff / 3600);//小时
$min_part = floor(($time_diff % 3600) / 60);//剩余分钟
if(!$min_part){
$res_string = $hour_part . ' ' . $hour . $ago;
}else{
$res_string = $hour_part . ' ' . $hour. ' ' . $min_part . ' ' .$minute . $ago;
}
}elseif ($time_diff < 604800){//1周内
$day_part = floor($time_diff / 86400);//天
$hour_part = floor(($time_diff % 86400) / 3600);//剩余分钟
if(!$hour_part){
$res_string = $day_part . ' ' . $day . $ago;
}else{
$res_string = $day_part . ' ' . $day. ' ' . $hour_part . ' ' . $hour . $ago;
}
}else{//大于1周
$res_string = sliceDT($time);
}
}
return $res_string;
}
function check_DT($date_time,$format='Y-m-d H:i:s'){
if(date($format, strtotime($date_time)) == $date_time){
return true;
}
return false;
}
function sliceDT($date_time){
$res_string = $date_time;
if(check_DT($date_time)){
$year = date('Y', strtotime($date_time));
$month = date('m', strtotime($date_time));
$day = date('d', strtotime($date_time));
$hour = date('H', strtotime($date_time));
$min = date('i', strtotime($date_time));
$sec = date('s', strtotime($date_time));
if(date('Y') == $year){
if(date('Ymd') == $year.$month.$day){
$res_string = $hour.':'.$min.':'.$sec;
}else{
$res_string = $month.'/'.$day.' '.$hour.':'.$min;
}
}else{
$res_string = $year.'/'.$month.'/'.$day;
}
}
return $res_string;
}
0人点赞>
请登录后发表评论
相关推荐
文章归档
2024-11
1 篇
2024-06
1 篇
2024-05
2 篇
2024-04
2 篇
2024-03
2 篇
展开剩余 68 条
2024-01
1 篇
2023-10
1 篇
2023-09
1 篇
2023-08
1 篇
2023-06
1 篇
2023-04
1 篇
2022-12
2 篇
2022-06
1 篇
2022-04
4 篇
2022-03
3 篇
2022-01
6 篇
2021-12
2 篇
2021-11
2 篇
2021-10
2 篇
2021-09
1 篇
2021-08
2 篇
2021-07
4 篇
2021-06
1 篇
2021-05
3 篇
2021-04
3 篇
2021-01
2 篇
2020-11
1 篇
2020-10
3 篇
2020-09
2 篇
2020-08
1 篇
2020-07
5 篇
2020-06
5 篇
2020-05
1 篇
2020-04
1 篇
2020-03
2 篇
2020-02
3 篇
2020-01
1 篇
2019-11
5 篇
2019-10
10 篇
2019-09
12 篇
2019-08
17 篇
2019-07
8 篇
2019-05
3 篇
2019-04
8 篇
2019-03
7 篇
2019-02
8 篇
2019-01
5 篇
2018-12
7 篇
2018-11
8 篇
2018-10
4 篇
2018-09
7 篇
2018-08
12 篇
2018-07
9 篇
2018-06
6 篇
2018-05
11 篇
2018-04
18 篇
2018-03
1 篇
2018-02
2 篇
2018-01
10 篇
2017-12
14 篇
2017-11
44 篇
2017-10
13 篇
2017-09
4 篇
2017-08
12 篇
2017-07
5 篇
2017-06
4 篇
2017-05
2 篇
2017-04
3 篇
2017-03
9 篇
2017-02
3 篇
2017-01
2 篇
2016-12
10 篇
2016-11
4 篇
最新文章
最受欢迎
11-07 19:00
06-26 11:51
05-17 17:08
05-17 10:59
04-11 17:05
13 评论
11 评论
10 评论
@saonian 嗯嗯,了解。了解。
很好,支持楼主。[em_13]