Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Adrian Sampson http://caprahircus.ws/

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Relative date

This function takes a time and returns a relative date string (it does, however, ignore the time of day). For instance: "yesterday", "in 2 days", "1 day ago", "March 12", or "May 5, 1992".

   1  function relative_date($time) {
   2      $today = strtotime(date('M j, Y'));
   3      $reldays = ($time - $today)/86400;
   4      if ($reldays >= 0 && $reldays < 1) {
   5          return 'today';
   6      } else if ($reldays >= 1 && $reldays < 2) {
   7          return 'tomorrow';
   8      } else if ($reldays >= -1 && $reldays < 0) {
   9          return 'yesterday';
  10      }
  11      if (abs($reldays) < 7) {
  12          if ($reldays > 0) {
  13              $reldays = floor($reldays);
  14              return 'in ' . $reldays . ' day' . ($reldays != 1 ? 's' : '');
  15          } else {
  16              $reldays = abs(floor($reldays));
  17              return $reldays . ' day'  . ($reldays != 1 ? 's' : '') . ' ago';
  18          }
  19      }
  20      if (abs($reldays) < 182) {
  21          return date('l, F j',$time ? $time : time());
  22      } else {
  23          return date('l, F j, Y',$time ? $time : time());
  24      }
  25  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS