PHP converts timestamps into formats that are several hours ago encapsulated

Source: Internet
Author: User
Tags sprintf

Method 1:

/** * Personalized Date display * @static * @access public * @param datetime $times Date * @return string return approximate date * @example example Uetime (") 
    */functionUetime ($times) {    if($times= = ' | |$times= = 0) {        return false; }    //Full time stamp    $strtotime=Is_int($times) ?$times:Strtotime($times); $times _day=Date(' y-m-d ',$strtotime); $times _day_strtotime=Strtotime($times _day); //Today    $nowdate _str=Strtotime(Date(' y-m-d ')); //precise time interval (seconds)    $interval= Time() -$strtotime; //today's    if($times _day_strtotime==$nowdate _str) {        //less than one minute        if($interval< 60) {            $pct=sprintf("%d seconds Ago",$interval); }        //less than 1 hours        ElseIf($interval< 3600) {            $pct=sprintf("%d minutes ago",Ceil($interval/60)); } Else {            $pct=sprintf("%d hours ago", Floor($interval/3600)); }    }    //yesterday's    ElseIf($times _day_strtotime==Strtotime(Date(' y-m-d ',Strtotime('-1 days '))))) {        $pct= ' Yesterday '.Date(' H:i ',$strtotime); }    //the day before yesterday    ElseIf($times _day_strtotime==Strtotime(Date(' y-m-d ',Strtotime('-2 days '))))) {        $pct= ' The day before yesterday '.Date(' H:i ',$strtotime); }    //one months or less    ElseIf($interval< (3600 * 24 * 30)) {        $pct=Date(' m ' D Day ',$strtotime); }    //within one year    ElseIf($interval< (3600 * 24 * 365)) {        $pct=Date(' m ' D Day ',$strtotime); }    //more than one year    Else {        $pct=Date(' Y year m D Day ',$strtotime); }    return $pct;}

Method 2:

<?PHPHeader("content-type:text/html; Charset=utf8 "); Date_default_timezone_set ("Asia/shanghai");//Setting the time zonefunctionTime_tran ($the _time) {      $now _time=Date("Y-m-d h:i:s", Time()); //echo $now _time;     $now _time=Strtotime($now _time); $show _time=Strtotime($the _time); $dur=$now _time-$show _time; if($dur< 0) {          return $the _time; } Else {          if($dur< 60) {              return $dur. ' Seconds ago '; } Else {              if($dur< 3600) {                  return  Floor($dur/60). ' Minutes ago '; } Else {                  if($dur< 86400) {                      return  Floor($dur/3600). ' Hour ago '; } Else {                      if($dur< 259200) {//within 3 days                        return  Floor($dur/86400). ' Days ago '; } Else {                          return $the _time; }                  }              }          }      }  }      EchoTime_tran ("2014-7-8 19:22:01"); ?>

Method 3:

functionWordtime ($time) {        $time= (int)substr($time, 0, 10); $int= Time() -$time; $str= ' '; if($int<= 2){            $str=sprintf(' Just ',$int); }ElseIf($int< 60){            $str=sprintf('%d seconds ago ',$int); }ElseIf($int< 3600){            $str=sprintf('%d minutes ago ', Floor($int/60)); }ElseIf($int< 86400){            $str=sprintf('%d hours ago ', Floor($int/3600)); }ElseIf($int< 2592000){            $str=sprintf('%d days ago ', Floor($int/86400)); }Else{            $str=Date(' Y-m-d h:i:s ',$time); }        return $str; }

PHP converts timestamps into formats that are several hours ago encapsulated

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.