Location: PHPKode > scripts > Prioritexter > Date.php
<?php
class Date {

  // how far in the future is end from start? or... is it in the past!?
  function diffToEnglish($start, $end) {
    //    return ($end-$start);
    $days_diff = round(Date::toDays($end-$start));
    
    switch ($days_diff) {
    case 0:
      $diff_phrase = "today";
    break;
    case 1:
      $diff_phrase = "tomorrow";
    break;
    case -1:
      $diff_phrase = "yesterday";
    break;
    }

    if (($days_diff>=2) && ($days_diff<=6))
      $diff_phrase = date("l", $end);

    if (($days_diff>=7) && ($days_diff<=11))
      $diff_phrase = "next ". date("l", $end);

    if (($days_diff>=-11) && ($days_diff<=-2))
      $diff_phrase = -1*$days_diff ." days ago";

    if ((abs($days_diff)>=12) && (abs($days_diff)<=16))
      $diff_phrase = "2 weeks";

    if ((abs($days_diff)>=17) && (abs($days_diff)<=19))
      $diff_phrase = "2 &#189 weeks";

    if ((abs($days_diff)>=20) && (abs($days_diff)<=25))
      $diff_phrase = "3 weeks";

    if ((abs($days_diff)>=26) && (abs($days_diff)<=34))
      $diff_phrase = "1 month";
    
    if ((abs($days_diff)>=35) && (abs($days_diff)<=38))
      $diff_phrase = "5 weeks";

    if ((abs($days_diff)>=39) && (abs($days_diff)<=48))
      $diff_phrase = "6 weeks";

    if ((abs($days_diff)>=49) && (abs($days_diff)<=335))
      $diff_phrase = round(abs($days_diff)/30.44) ." months";

    if ((abs($days_diff)>=336) && (abs($days_diff)<456))
      $diff_phrase = "1 year";

    if ((abs($days_diff)>=457) && (abs($days_diff)<=639))
      $diff_phrase = "18 months";

    if (abs($days_diff)>=640)
      $diff_phrase = round(abs($days_diff)/365.25) ." years";

    if ($days_diff<=-12)
      $diff_phrase .= " ago";

    return $diff_phrase;
  }

  function toDays($time) {
    return ($time/86400);
  }
    
  function testEnglish() {
    for ($i=0; $i<20; $i++) {
      $offset = 86400 * rand(-50,50);
      $offset += 86400 * rand(-50,50);
      $end = time() + $offset;
      $diff_str = Date::diffToEnglish(time(), $end);
      echo "<p>end date: ". date("l F jS, Y",$end);
      echo "<br>phrase: <font color=\"". Date::diffToColor(time(), $end) ."\">$diff_str</font>";
    }    
  }

  function diffToColor($start, $end) {
    $days_diff = round(Date::toDays($end-$start));
      
    if ($days_diff>7) return "#777777";
    elseif ($days_diff>0) return "#5555bb";
    elseif ($days_diff==0) return "#00bb00";
    else return "#ee4444";
  }

  // remember, date needs to be interpreted as 3 or 4pm in the 
  // afternoon.
  function interpretDate($str) {
    $date = strtotime($str);
    $date += 3600*16;
    return $date;
  }

  function logicalTime($raw_time=0) {
    if ($raw_time==0) $raw_time = time();
    // first add the adjustment to bring time() from GMT to local;
    $hours_adjusted_time = $raw_time  +HOURS_ADJUSTMENT*3600;
    // pretend midnight to 4am is part of the previous day:
    // subtract 4 hours to get time within current gmt "day".
    $time_adjusted_for_extended_day = $hours_adjusted_time
       -3600*4;
    // get how far into today we are
    $time_into_today = bcmod($time_adjusted_for_extended_day, 86400);
    // get start of today (theoretically 12:01 am this prev morning)
    $start_of_today = $time_adjusted_for_extended_day-$time_into_today;
    // set logical current time to middle of adjusted day
    $logical_time = $start_of_today + 12*3600 + 4*3600 
       - HOURS_ADJUSTMENT*3600;
    Debug::debug("time $raw_time was adjusted to "
		 . $time_adjusted_for_extended_day
		 . " and interpreted as $logical_time logically.");
    return $logical_time;
  }


}
?>
Return current item: Prioritexter