PHP Get time difference

Source: Internet
Author: User
PHP Gets the time difference, we are in the process of development, this is a frequently used function, for example, we want to check the data for a certain period, it is necessary to use this function.


Example one:

/** * Function: Get time difference * @param int $time * @return string time difference value */function trantime ($time) {    $rtime = date ("m-d h:i", $time); 
   $htime = Date ("H:i", $time);     $time = Time ()-$time;     if ($time <) {        $str = ' just ';    }    ElseIf ($time <) {        $min = floor ($time/60);        $str = $min. ' Minutes ago ';    }    ElseIf ($time < *) {        $h = floor ($time/(60*60));        $str = $h. ' Hour ago '. $htime;    }    ElseIf ($time < * 3) {        $d = floor ($time/(60*60*24));        if ($d ==1)           $str = ' Yesterday '. $rtime;        else           $str = ' The day before yesterday '. $rtime;    }    else {        $str = $rtime;    }    return $STR;}

Example two:

function Time_tran ($the _time) {   $now _time = Date ("Y-m-d h:i:s", Time () +8*60*60);   $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) {      return floor ($dur/60). ' Minutes ago ';     } else{      if ($dur < 86400) {       return floor ($dur/3600). ' Hours ago ';      } else{       if ($dur < 259200) {//3 days        return floor ($dur/86400). ' Days ago ';       } else{        return $the _time;}}}      

Example three:

function Format_date ($time) {    $t =time ()-$time;    $f =array (        ' 31536000 ' = ' year ',        ' 2592000 ' + ' month ', '        604800 ' + ' ' Week ',        ' 86400 ' and ' Day ',        ' 3600 ' = ' + ' hours ', ' + '        = ' minutes ',        ' 1 ' = ' s '    ;    foreach ($f as $k = + $v)    {        if (0! = $c =floor ($t/(int) $k)) {            return $c. $v. ' ago ';        }    }

Example four:

function Formattime ($date) {$str = '; $timer = Strtotime ($date); $diff = $_server[' Request_time ']-$timer; $day = Floor ($di ff/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/60), $free = $free% 60;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 five:

function Time_tran ($the _time) {$now _time = date ("Y-m-d h:i:s", Time () +8*60*60), $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) {   return floor ($dur/60). ' Minutes ago ';     } else{   if ($dur < 86400) {   return floor ($dur/3600). ' Hours ago ';    } else{   if ($dur < 259200) {//3 days       return floor ($dur/86400). ' Days ago ';   } else{       return $the _time;}}}}}    

Example SIX:

/* * Author:solon Ring * time:2011-11-02 * bo Time calculation (year, month, day, hour, minute, seconds) * $createtime can be the current time * $gettime you want to pass in the time */class Mygetti            Me{function __construct ($createtime, $gettime) {$this->createtime = $createtime;    $this->gettime = $gettime;        }function getseconds () {return $this->createtime-$this->gettime;       }function getminutes () {return ($this->createtime-$this->gettime)/(60);       }function getHours () {return ($this->createtime-$this->gettime)/(60*60);       }function GetDay () {return ($this->createtime-$this->gettime)/(60*60*24);       }function GetMonth () {return ($this->createtime-$this->gettime)/(60*60*24*30);       }function GetYear () {return ($this->createtime-$this->gettime)/(60*60*24*30*12); }function Index () {if ($this->getyear () > 1) {if ($this->getyear () &G T           2)         {return date ("y-m-d", $this->gettime);                    Exit ();                } return Intval ($this->getyear ()). "Years ago";            Exit ();                }if ($this->getmonth () > 1) {return intval ($this->getmonth ()). "Months ago";            Exit ();                }if ($this->getday () > 1) {return intval ($this->getday ()). "Days Ago";            Exit ();                }if ($this->gethours () > 1) {return intval ($this->gethours ()). "Hours ago";            Exit ();                }if ($this->getminutes () > 1) {return intval ($this->getminutes ()). "Minutes Ago";            Exit ();                }if ($this->getseconds () > 1) {return intval ($this->getseconds ()-1). "Seconds Ago";            Exit (); Use instances of the}}}//class/* * Call class Output mode * * $a = new Mygettime (Time (), Strtotime (' -25 month ')); * ECHo iconv (' utf-8 ', ' gb2312 ', $a->index ())? Iconv (' Utf-8 ', ' gb2312 ', $a->index ()): Iconv (' utf-8 ', ' gb2312 ', ' current '); * */
  • 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.