PHP calculates a few ago, hours ago, days ago several functions

Source: Internet
Author: User

First, the function realization

Example 1:

function Time_tran ($the _time) {$now _time= Date ("y-m-d h:i:s", Time () +8* -* -); $now _time=strtotime ($now _time); $show _time=strtotime ($the _time); $dur= $now _time-$show _time; if($dur <0){    return$the _time; }Else{    if($dur < -){     return$dur.'seconds ago'; }Else{     if($dur <3600){      returnFloor ($dur/ -).'minutes ago'; }Else{      if($dur <86400){       returnFloor ($dur/3600).'hours ago'; }Else{       if($dur <259200){//within 3 days        returnFloor ($dur/86400).'days ago'; }Else{        return$the _time; }      } } 

Example 2:

<?phpfunction format_date ($time) {$t=time ()-$time; $f=Array ('31536000'='years',        '2592000'='a month',        '604800'='Week',        '86400'='days',        '3600'='hours',        ' -'='minutes',        '1'='seconds'    ); foreach($f as$k =$v) {        if(0! = $c =floor ($t/(int) ( $k)) {return$c. $v.'before'; }    }}

Example 3:

function Formattime ($date) {$str="'; $timer=Strtotime ($date); $diff= $_server['Request_time'] -$timer; $day= Floor ($diff/86400); $free= $diff%86400;if($day >0) {return$day."days ago";}Else{if($free >0) {$hour= Floor ($free/3600); $free= $free%3600;if($hour >0){return$hour."hours ago";}Else{if($free >0) {$min= Floor ($free/ -); $free= $free% -;if($min >0){return$min."minutes ago";}Else{if($free >0){return$free."seconds ago";}Else{return 'just';}}}Else{return 'just';}}}Else{return 'just';}}}

Example 4:

function Time_tran ($the _time) {$now _time= Date ("y-m-d h:i:s", Time () +8* -* -); $now _time=strtotime ($now _time); $show _time=strtotime ($the _time); $dur= $now _time-$show _time;if($dur <0){return$the _time;}Else{if($dur < -){    return$dur.'seconds ago'; }Else{    if($dur <3600){   returnFloor ($dur/ -).'minutes ago'; }Else{   if($dur <86400){   returnFloor ($dur/3600).'hours ago'; }Else{   if($dur <259200){//within 3 days       returnFloor ($dur/86400).'days ago'; }Else{       return$the _time; }   }    }}}}

PHP calculates a few ago, hours ago, days ago several functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.