Home  >  Article  >  Backend Development  >  How to get a few minutes ago in php

How to get a few minutes ago in php

藏色散人
藏色散人Original
2022-12-15 11:32:455605browse

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.

How to get a few minutes ago in php

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 = &#39;刚刚&#39;; 
    } 
    elseif ($time < 60 * 60) { 
        $min = floor($time/60); 
        $str = $min.&#39;分钟前&#39;; 
    } 
    elseif ($time < 60 * 60 * 24) { 
        $h = floor($time/(60*60)); 
        $str = $h.&#39;小时前 &#39;.$htime; 
    } 
    elseif ($time < 60 * 60 * 24 * 3) { 
        $d = floor($time/(60*60*24)); 
        if($d==1) 
           $str = &#39;昨天 &#39;.$rtime; 
        else 
           $str = &#39;前天 &#39;.$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!

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