bikerabhinav

DateTime script

Jun 22nd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. //$date_ref is in this format date('Y-m-d H:i:s')
  2. function dateTimeDiff($date_ref){
  3. // Get the current date
  4. $current_date = date('Y-m-d H:i:s');
  5.  
  6.     $y1 = substr($date_ref,0,4);
  7.     $m1 = substr($date_ref,5,2);
  8.     $d1 = substr($date_ref,8,2);
  9.     $h1 = substr($date_ref,11,2);
  10.     $i1 = substr($date_ref,14,2);
  11.     $s1 = substr($date_ref,17,2);    
  12.  
  13.     $y2 = substr($current_date,0,4);
  14.     $m2 = substr($current_date,5,2);
  15.     $d2 = substr($current_date,8,2);
  16.     $h2 = substr($current_date,11,2);
  17.     $i2 = substr($current_date,14,2);
  18.     $s2 = substr($current_date,17,2);
  19.  
  20.     $r1=date('U',mktime($h1,$i1,$s1,$m1,$d1,$y1));
  21.     $r2=date('U',mktime($h2,$i2,$s2,$m2,$d2,$y2));
  22.  
  23.     $sDf = $r2 - $r1;
  24.     $mDf = floor($sDf/60);
  25.     $hDf = floor($sDf/3600);
  26.     $dDf = floor($hDf/24);
  27.     $mnDf = floor($dDf/30);
  28.     $yDf = floor($mnDf/12);
  29.  
  30.     if($yDf) {
  31.         if($yDf<2)
  32.             echo $yDf . " year ago";
  33.         else
  34.             echo $yDf . " years ago";
  35.     }
  36.     elseif($mnDf) {
  37.         if($mnDf<2)
  38.             echo $mnDf . " month ago";
  39.         else
  40.             echo $mnDf . " months ago";
  41.     }
  42.     elseif($dDf) {
  43.         if($dDf<2)
  44.             echo "yesterday";
  45.         else
  46.             echo $dDf . " days ago";
  47.     }
  48.     elseif($hDf) {
  49.         if($hDf<2)
  50.             echo $hDf . " hour ago";
  51.         else
  52.             echo $hDf . " hours ago";
  53.     }
  54.     elseif($mDf) {
  55.         if($mDf<2)
  56.             echo $mDf . " minute ago";
  57.         else
  58.             echo $mDf . " minutes ago";
  59.     }
  60.     else {
  61.         echo $sDf . " seconds ago";
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment