Home >Backend Development >PHP Tutorial >How to change timestamp to normal time format in php language? (specific example)
The concept of php timestamp must be familiar to those who are just getting started. We will all think of two very important time functions Date/Time at the first time. First, let me briefly introduce the relevant knowledge of these two functions.
The Date/Time function allows you to get the date and time from the server where the PHP script is running. You can use these two functions to format dates and times in different ways. Note here that they are dependent on the server's local settings, and remember to take daylight saving time and leap years into account when using these functions.
Then this article is to give you a detailed introduction to the specific usage of PHP time to minute conversion (hours, days...) and how to convert it into the time format of a few minutes ago. (Hours, days...)
1. The specific code examples for converting PHP time to minutes (days, hours) are as follows:
function format_date($time){ if(!is_numeric($time)){ $time=strtotime($time); } $t=time()-$time; $f=array( '31536000'=>'年', '2592000'=>'个月', '604800'=>'星期', '86400'=>'天', '3600'=>'小时', '60'=>'分钟', '1'=>'秒' ); foreach ($f as $k=>$v) { if (0 !=$c=floor($t/(int)$k)) { return '<span class="pink">'.$c.' </span>'.$v.'前'; } } }
2. The specific code example of how many minutes (days, hours) the PHP timestamp is converted into is as follows:
function get_last_time($time) { $todayLast = strtotime(date('Y-m-d 23:59:59')); $agoTimeTrue = time() - $time; $agoTime = $todayLast - $time; $agoDay = floor($agoTime / 86400); if ($agoTimeTrue < 60) { $result = '刚刚'; } elseif ($agoTimeTrue < 3600) { $result = (ceil($agoTimeTrue / 60)) . '分钟前'; } elseif ($agoTimeTrue < 3600 * 12) { $result = (ceil($agoTimeTrue / 3600)) . '小时前'; } elseif ($agoDay == 1) { $result = '昨天 '; } elseif ($agoDay == 2) { $result = '前天 '; } else { $format = date('Y') != date('Y', $time) ? "Y-m-d" : "m-d"; $result = date($format, $time); } return $result; }
Note: function get_last_time(){} Gets the maximum time of the day
Pass this article This article introduces PHP time-minute conversion (days, hours) and other related knowledge. I hope it will be helpful to friends in need!
【Recommended related articles】
Detailed explanation of the PHP function to obtain the current timestamp
PHP time() function acquisition Detailed explanation of the current timestamp example
How PHP obtains the zero o’clock timestamp of the day
php Method example code for obtaining millisecond-level timestamp
The above is the detailed content of How to change timestamp to normal time format in php language? (specific example). For more information, please follow other related articles on the PHP Chinese website!