Home >Backend Development >PHP Tutorial >How to change timestamp to normal time format in php language? (specific example)

How to change timestamp to normal time format in php language? (specific example)

藏色散人
藏色散人Original
2018-08-14 16:35:372984browse

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 &#39;<span class="pink">&#39;.$c.&#39; </span>&#39;.$v.&#39;前&#39;;
        }
    }
}

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(&#39;Y-m-d 23:59:59&#39;));
$agoTimeTrue = time() - $time;
$agoTime = $todayLast - $time;
$agoDay = floor($agoTime / 86400);
if ($agoTimeTrue < 60) {
$result = &#39;刚刚&#39;;
} elseif ($agoTimeTrue < 3600) {
$result = (ceil($agoTimeTrue / 60)) . &#39;分钟前&#39;;
} elseif ($agoTimeTrue < 3600 * 12) {
$result = (ceil($agoTimeTrue / 3600)) . &#39;小时前&#39;;
} elseif ($agoDay == 1) {
$result = &#39;昨天 &#39;;
} elseif ($agoDay == 2) {
$result = &#39;前天 &#39;;
} else {
$format = date(&#39;Y&#39;) != date(&#39;Y&#39;, $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!

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