How to get time difference in minutes in PHP

后端 未结 17 2300
独厮守ぢ
独厮守ぢ 2020-11-21 07:22

How to calculate minute difference between two date-times in PHP?

17条回答
  •  梦谈多话
    2020-11-21 08:02

    I wrote this function for one my blog site(difference between a past date and server's date). It will give you an output like this

    "49 seconds ago", "20 minutes ago", "21 hours ago" and so on

    I have used a function that would get me the difference between the date passed and the server's date.

    format('%s Seconds %i Minutes %h Hours %d days %m Months %y Year    Ago')."
    "; $min=$interval->format('%i'); $sec=$interval->format('%s'); $hour=$interval->format('%h'); $mon=$interval->format('%m'); $day=$interval->format('%d'); $year=$interval->format('%y'); if($interval->format('%i%h%d%m%y')=="00000") { //echo $interval->format('%i%h%d%m%y')."
    "; return $sec." Seconds"; } else if($interval->format('%h%d%m%y')=="0000"){ return $min." Minutes"; } else if($interval->format('%d%m%y')=="000"){ return $hour." Hours"; } else if($interval->format('%m%y')=="00"){ return $day." Days"; } else if($interval->format('%y')=="0"){ return $mon." Months"; } else{ return $year." Years"; } } ?>

    Save it as a file suppose "date.php". Call the function from another page like this

     ";
     echo dateDiff($mydate);
    ?>
    

    Of course you can modify the function to pass two values.

提交回复
热议问题