Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //$date_ref is in this format date('Y-m-d H:i:s')
- function dateTimeDiff($date_ref){
- // Get the current date
- $current_date = date('Y-m-d H:i:s');
- $y1 = substr($date_ref,0,4);
- $m1 = substr($date_ref,5,2);
- $d1 = substr($date_ref,8,2);
- $h1 = substr($date_ref,11,2);
- $i1 = substr($date_ref,14,2);
- $s1 = substr($date_ref,17,2);
- $y2 = substr($current_date,0,4);
- $m2 = substr($current_date,5,2);
- $d2 = substr($current_date,8,2);
- $h2 = substr($current_date,11,2);
- $i2 = substr($current_date,14,2);
- $s2 = substr($current_date,17,2);
- $r1=date('U',mktime($h1,$i1,$s1,$m1,$d1,$y1));
- $r2=date('U',mktime($h2,$i2,$s2,$m2,$d2,$y2));
- $sDf = $r2 - $r1;
- $mDf = floor($sDf/60);
- $hDf = floor($sDf/3600);
- $dDf = floor($hDf/24);
- $mnDf = floor($dDf/30);
- $yDf = floor($mnDf/12);
- if($yDf) {
- if($yDf<2)
- echo $yDf . " year ago";
- else
- echo $yDf . " years ago";
- }
- elseif($mnDf) {
- if($mnDf<2)
- echo $mnDf . " month ago";
- else
- echo $mnDf . " months ago";
- }
- elseif($dDf) {
- if($dDf<2)
- echo "yesterday";
- else
- echo $dDf . " days ago";
- }
- elseif($hDf) {
- if($hDf<2)
- echo $hDf . " hour ago";
- else
- echo $hDf . " hours ago";
- }
- elseif($mDf) {
- if($mDf<2)
- echo $mDf . " minute ago";
- else
- echo $mDf . " minutes ago";
- }
- else {
- echo $sDf . " seconds ago";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment