header

Relative Date vs Absolute Date, Relative Date with PHP



One thing that I’ve been noticing a lot recently is the usage of relative dates instead of absolute dates. In other words, dates of objects are formatted as “X days ago”, “X minutes ago” or “X weeks ago” instead of “5:32 p.m. October 12, 2006”.

I’m sure that each of these has applications where they are more appropriate. However, if you format a date absolutely, you need to have the time zone, and whether or not daylight savings time is enabled on the client side. Even so, it is prone to error. For example, if the server’s time is different than the client’s then they would see an inevitable difference. I.e. “I just created an object on this website, why does it have the timestamp of X minutes in the future?”

So, I would argue that more often than not, relative is better than absolute. Awhile back, I created a thread notifier script which utilizes a relative date difference function in PHP. I honestly don’t remember where I found it, but if it’s YOURS, let me know and I can give you credit.

// this function determines the difference between two unix timestamps
function dateDiff($dateTimeBegin,$dateTimeEnd) {
 // $dateTimeBegin =strtotime($dateTimeBegin);
 // $dateTimeEnd  =strtotime($dateTimeEnd);
     
  if($dateTimeEnd === -1 || $dateTimeBegin === -1) {
   # error condition
   return false;
  }
    
  $diff = $dateTimeEnd - $dateTimeBegin;
   
  if ($diff < 0) {
   # error condition
   return false;
  }
   
  $weeks = $days = $hours = $minutes = $seconds = 0; # initialize vars
   
  if($diff % 604800 > 0) {
   $rest1  = $diff % 604800;
   $weeks  = ($diff - $rest1) / 604800; # seconds a week
   if($rest1 % 86400 > 0) {
     $rest2 = ($rest1 % 86400);
     $days  = ($rest1 - $rest2) / 86400; # seconds a day
     if( $rest2 % 3600 > 0 ) {
       $rest3 = ($rest2 % 3600);
       $hours = ($rest2 - $rest3) / 3600; # seconds an hour
       if( $rest3 % 60 > 0 ) {
         $seconds = ($rest3 % 60);
         $minutes = ($rest3 - $seconds) / 60;  # seconds a minute
       } else {
         $minutes = $rest3 / 60;
       }
     } else {
       $hours = $rest2 / 3600;
     }
   } else {
     $days = $rest1/ 86400;
   }
  }else {
   $weeks = $diff / 604800;
  }

  $string = array();
  if($weeks > 1) {
   $string[]  = "$weeks weeks";
  } elseif ($weeks == 1) {
   $string[]  = "a week";
  }
  if($days > 1) {
   $string[] = "$days days";
  } elseif($days == 1) {
   $string[] = "a day";
  }
  if($hours > 1) {
   $string[] = "$hours hours";
  } elseif ($hours == 1) {
   $string[] = "an hour";
  }
  if($minutes > 1) {
   $string[] = "$minutes minutes";
  } elseif ($minutes == 1) {
   $string[] = "a minute";
  }
  if($seconds > 1) {
   $string[] = "$seconds seconds";
  } elseif($seconds == 1) {
   $string[] = "a second";
  }

  # join together all the strings in the array above except the last element
  //$text  = join(', ', array_slice($string,0,sizeof($string)-1)) . ", and ";
  //$text .= array_pop($string);  # put the last one on after the and
  //i only want the most significant date increment
  $text = $string[0];

  return array($text, $weeks, $days, $hours, $minutes, $seconds);
}

I know I’ll use this a lot in the future. Please let me know if YOU use it.



2 Comments »

  1. gravatar

    Dustin Said,

    December 6, 2006 @ 6:06 pm

    I don’t remember the name but I thought PHP had a time difference function built in.

  2. gravatar

    Raybdbomb Said,

    December 6, 2006 @ 9:34 pm

    You’re probably thinking of mktime. I want the inverse of that, slightly edited.

RSS feed for comments on this post · TrackBack URI

Leave a Comment

blogtimes