Home > Article > Backend Development > How to get a few minutes ago in php
php Get the implementation method from a few minutes ago: 1. Create a PHP sample file; 2. Use function to define a tranTime method; 3. Get and calculate the time difference through date, time and floor functions in the method body ;4. Run the file and output the results displayed a few minutes ago.
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, Dell G3 computer.
php How to get a few minutes ago?
PHP Get the function a few minutes ago
The code is as follows:
/** +---------------------------------------------------------- * 功能:获取时间差 +---------------------------------------------------------- * @param int $time +---------------------------------------------------------- * @return string 时间差值 +---------------------------------------------------------- */ function tranTime($time) { $rtime = date("m-d H:i",$time); $htime = date("H:i",$time); $time = time() - $time; if ($time < 60) { $str = '刚刚'; } elseif ($time < 60 * 60) { $min = floor($time/60); $str = $min.'分钟前'; } elseif ($time < 60 * 60 * 24) { $h = floor($time/(60*60)); $str = $h.'小时前 '.$htime; } elseif ($time < 60 * 60 * 24 * 3) { $d = floor($time/(60*60*24)); if($d==1) $str = '昨天 '.$rtime; else $str = '前天 '.$rtime; } else { $str = $rtime; } return $str; }
Related function introduction:
PHP date()
This function formats the timestamp into a more readable date and time.
time()
The function returns the number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT) of the current time.
floor()
The function rounds down to the nearest integer.
Tip: To round up to the nearest integer, check out the ceil() function.
Tip: If you need to round floating point numbers, check out the round() function.
Syntax
floor(number);
Parameter number is required and specifies the value that needs to be rounded down.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get a few minutes ago in php. For more information, please follow other related articles on the PHP Chinese website!