Changeset 13085


Ignore:
Timestamp:
Feb 10, 2012, 5:11:50 PM (13 years ago)
Author:
mistic100
Message:

feature 2564: forgot a file in previous commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions.inc.php

    r12954 r13085  
    493493//
    494494// format_date( "2003-09-15", true ) -> "Monday 15 September 2003 21:52"
    495 function format_date($date, $show_time = false)
     495function format_date($date, $show_time = false, $show_day_name = true)
    496496{
    497497  global $lang;
     
    517517  $formated_date = '';
    518518  // before 1970, Microsoft Windows can't mktime
    519   if ($ymdhms[0] >= 1970)
     519  if ($show_day_name and $ymdhms[0] >= 1970)
    520520  {
    521521    // we ask midday because Windows think it's prior to midnight with a
     
    531531  }
    532532  return $formated_date;
     533}
     534
     535/**
     536 * Works out the time since the entry post, takes a an argument in unix time or datetime
     537 */
     538function time_since($original, $stop = 'minute')
     539{
     540  if (!is_int($original))
     541  {
     542    $ymdhms = array();
     543    $tok = strtok($original, '- :');
     544    while ($tok !== false)
     545    {
     546      $ymdhms[] = $tok;
     547      $tok = strtok('- :');
     548    }
     549    $original = mktime($ymdhms[3],$ymdhms[4],$ymdhms[5],$ymdhms[1],$ymdhms[2],$ymdhms[0]);
     550  }
     551 
     552  // array of time period chunks
     553  $chunks = array(
     554      array(60 * 60 * 24 * 365 , 'year'),
     555      array(60 * 60 * 24 * 30 , 'month'),
     556      array(60 * 60 * 24 * 7, 'week'),
     557      array(60 * 60 * 24 , 'day'),
     558      array(60 * 60 , 'hour'),
     559      array(60 , 'minute'),
     560      array(1 , 'second'),
     561  );
     562 
     563  $today = time(); /* Current unix time  */
     564  $since = abs($today - $original);
     565 
     566  $print = null;
     567  for ($i = 0, $j = count($chunks); $i < $j; $i++)
     568  {
     569    $seconds = $chunks[$i][0];
     570    $name = $chunks[$i][1];
     571    if (($count = floor($since / $seconds)) != 0)
     572    {
     573      $print.= ($count == 1) ? '1 '.l10n($name).' ' : $count.' '.l10n($name.'s').' ';
     574      $since-= $count*$seconds;
     575    }
     576    if ($name == $stop)
     577    {
     578      break;
     579    }
     580  }
     581
     582  if ($today > $original)
     583  {
     584    $print = sprintf(l10n('%s ago'), $print);
     585  }
     586  else
     587  {
     588    $print = sprintf(l10n('%s in the future'), $print);
     589  }
     590 
     591  return $print;
    533592}
    534593
Note: See TracChangeset for help on using the changeset viewer.