Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php timestamp formatting displays friendly time functions

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces php timestamp format how to display friendly time function, the content is very detailed, interested friends can refer to reference, hope to be helpful to everyone.

The general idea is as follows:

If it's a new year and more than 3 days, it's displayed as a specific time.

If it's today's

If it's within a minute, it shows seconds ago.

If it's within an hour, it shows minutes ago.

If it's today and more than an hour, it's hours ago.

If it was yesterday, what time was it yesterday?

If it's the day before yesterday, what time is it?

If it is longer than three days (no new year), it will be displayed as the month and day.

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

The implementation code is as follows:

The copy code is as follows:

//Format friendly display time

function formatTime($time){

$now=time();

$day=date('Y-m-d',$time);

$today=date('Y-m-d');

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

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

//Days of distance. This method is not necessarily accurate beyond 30 days, but it is accurate within 30 days, because a month may be 30 days or 31 days.

$days=($todayArr[0]-$dayArr[0])*365+(($todayArr[1]-$dayArr[1])*30)+($todayArr[2]-$dayArr[2]);

//Seconds of distance

$secs=$now-$time;

if($todayArr[0]-$dayArr[0]>0 && $days>3){//New Year and more than 3 days

return date('Y-m-d',$time);

}else{

if($days

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report