Home >Backend Development >PHP Tutorial >PHP formatted timestamp display friendly time implementation ideas and codes, PHP ideas_PHP tutorial

PHP formatted timestamp display friendly time implementation ideas and codes, PHP ideas_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:291035browse

php formatted timestamp display friendly time implementation ideas and codes, php ideas

The time in the project is always displayed as 2014-10-20 10:22, which seems very dull. On websites such as Weibo and QQ Space, the time is usually displayed as a few seconds ago, a few minutes ago, a few hours ago, etc. that are easy to read. We call this a friendly time format. So how to implement it using php?

The general idea is as follows:

If it is a New Year's Eve and it is more than 3 days, it will be displayed as the specific time

If it’s today

If it is within one minute, it will show how many seconds ago it was

If it is within one hour, it will display a few minutes ago

If it is the current day and is greater than one hour, it will be displayed as a few hours ago

If it is yesterday, the time will be displayed as yesterday

If it is the day before yesterday, it will display the time the day before yesterday

If it is more than three days (no New Year span), it will display the day of the month

Based on the above ideas, it is not difficult to write the implementation code:

The implementation code is as follows:

//格式化友好显示时间
function formatTime($time){
$now=time();
$day=date('Y-m-d',$time);
$today=date('Y-m-d');

$dayArr=explode('-',$day);
$todayArr=explode('-',$today);

//距离的天数,这种方法超过30天则不一定准确,但是30天内是准确的,因为一个月可能是30天也可能是31天
$days=($todayArr[0]-$dayArr[0])*365+(($todayArr[1]-$dayArr[1])*30)+($todayArr[2]-$dayArr[2]);
//距离的秒数
$secs=$now-$time;

if($todayArr[0]-$dayArr[0]>0 && $days>3){//跨年且超过3天
return date('Y-m-d',$time);
}else{

if($days<1){//今天
if($secs<60)return $secs.'秒前';
elseif($secs<3600)return floor($secs/60)."分钟前";
else return floor($secs/3600)."小时前";
}else if($days<2){//昨天
$hour=date('h',$time);
return "昨天".$hour.'点';
}elseif($days<3){//前天
$hour=date('h',$time);
return "前天".$hour.'点';
}else{//三天前
return date('m月d号',$time);
}
}
}

Code to convert PHP time to timestamp

Available in Chinese. You need to use regular expressions to replace Chinese characters. Because that's not a standard format.

can be processed with the following code.

$date = "15:12 on November 1, 2009"; //Must be a standard double-digit date
$date = ereg_replace("[^0-9]","", $date); //Filter non-numbers
$time = strtotime($date); //The variable $time is the timestamp

However. If you are using the current time. It's faster to just write like this.
$time = time();

The php date function can format the timestamp into 2012-5-3

date('Y-n-j', time()); // The time() function generates a timestamp of the current time, which can be replaced with your own timestamp

Reference: cn2.php .net/manual/zh/function.date.php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/898281.htmlTechArticlePHP formatted timestamp displays friendly time implementation ideas and codes. In PHP ideas, the time in the project is always displayed as 2014 -10-20 10:22 Looks very dull. On websites such as Weibo and QQ space, it usually appears...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn