Changeset 24099 for trunk/include


Ignore:
Timestamp:
Aug 1, 2013, 7:03:57 PM (11 years ago)
Author:
mistic100
Message:

bug:2947 rewrite format_date() and time_since() with DateTime class + new parameters

File:
1 edited

Legend:

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

    r24009 r24099  
    458458}
    459459
    460 // format_date returns a formatted date for display. The date given in
    461 // argument must be an american format (2003-09-15). By option, you can show the time.
    462 // The output is internationalized.
    463 //
    464 // format_date( "2003-09-15", true ) -> "Monday 15 September 2003 21:52"
    465 function format_date($date, $show_time = false, $show_day_name = true)
     460/**
     461 * converts a string into a DateTime object
     462 * @param: mixed, datetime string or timestamp int
     463 * @param: string, input format
     464 * @return: DateTime or false
     465 */
     466function str2DateTime($original, $format=null)
     467{
     468  if (!empty($format))// from known date format
     469  {
     470    return DateTime::createFromFormat('!'.$format, $original); // ! char to reset fields to UNIX epoch
     471  }
     472  else
     473  {
     474    $date = new DateTime();
     475   
     476    $t = trim($original, '0123456789');
     477    if (empty($t)) // from timestamp
     478    {
     479      $date->setTimestamp($original);
     480    }
     481    else // from unknown date format (assuming something like Y-m-d H:i:s)
     482    {
     483      $ymdhms = array();
     484      $tok = strtok($original, '- :/');
     485      while ($tok !== false)
     486      {
     487        $ymdhms[] = $tok;
     488        $tok = strtok('- :/');
     489      }
     490     
     491      if (count($ymdhms)<3) return false;
     492      if (!isset($ymdhms[3])) $ymdhms[3] = 0;
     493      if (!isset($ymdhms[4])) $ymdhms[4] = 0;
     494      if (!isset($ymdhms[5])) $ymdhms[5] = 0;
     495     
     496      $date->setDate($ymdhms[0], $ymdhms[1], $ymdhms[2]);
     497      $date->setTime($ymdhms[3], $ymdhms[4], $ymdhms[5]);
     498    }
     499   
     500    return $date;
     501  }
     502}
     503
     504/**
     505 * returns a formatted date for display
     506 * @param: mixed, datetime string or timestamp int
     507 * @param: bool, show time
     508 * @param: bool, show day name
     509 * @param: string, input format
     510 * @return: string
     511 */
     512function format_date($original, $show_time=false, $show_day_name=true, $format=null)
    466513{
    467514  global $lang;
    468 
    469   if (strpos($date, '0') == 0)
     515 
     516  $date = str2DateTime($original, $format);
     517
     518  if (!$date)
    470519  {
    471520    return l10n('N/A');
    472521  }
    473522
    474   $ymdhms = array();
    475   $tok = strtok( $date, '- :');
    476   while ($tok !== false)
    477   {
    478     $ymdhms[] = $tok;
    479     $tok = strtok('- :');
    480   }
    481 
    482   if ( count($ymdhms)<3 )
    483   {
    484     return false;
    485   }
    486 
    487   $formated_date = '';
    488   // before 1970, Microsoft Windows can't mktime
    489   if ($ymdhms[0] >= 1970 and $ymdhms[1] != 0 and $ymdhms[2] != 0)
    490   {
    491     // we ask midday because Windows think it's prior to midnight with a
    492     // zero and refuse to work
    493     $formated_date.= $lang['day'][date('w', mktime(12,0,0,$ymdhms[1],$ymdhms[2],$ymdhms[0]))];
    494   }
    495 
    496   if ($ymdhms[2] != 0)
    497   {
    498     $formated_date.= ' '.$ymdhms[2];
    499   }
    500 
    501   if ($ymdhms[1] != 0)
    502   {
    503     $formated_date.= ' '.$lang['month'][(int)$ymdhms[1]];
    504   }
    505 
    506   $formated_date.= ' '.$ymdhms[0];
    507   if ($show_time and count($ymdhms)>=5 )
    508   {
    509     $formated_date.= ' '.$ymdhms[3].':'.$ymdhms[4];
    510   }
    511   return $formated_date;
    512 }
    513 
    514 /**
    515  * Works out the time since the entry post, takes a an argument in unix time or datetime
    516  */
    517 function time_since($original, $stop = 'minute')
    518 {
    519   if (!is_int($original))
    520   {
    521     $ymdhms = array();
    522     $tok = strtok($original, '- :');
    523     while ($tok !== false)
    524     {
    525       $ymdhms[] = $tok;
    526       $tok = strtok('- :');
    527     }
    528 
    529     if ($ymdhms[0] < 1970) return false;
    530     if (!isset($ymdhms[3])) $ymdhms[3] = 12;
    531     if (!isset($ymdhms[4])) $ymdhms[4] = 0;
    532     if (!isset($ymdhms[5])) $ymdhms[5] = 0;
    533     $original = mktime($ymdhms[3],$ymdhms[4],$ymdhms[5],$ymdhms[1],$ymdhms[2],$ymdhms[0]);
    534   }
    535 
    536   // array of time period chunks
     523  $print = '';
     524  if ($show_day_name)
     525  {
     526    $print.= $lang['day'][ $date->format('w') ];
     527  }
     528 
     529  $print.= ' '.$date->format('d');
     530  $print.= ' '.$lang['month'][ $date->format('n') ];
     531  $print.= ' '.$date->format('Y');
     532 
     533  if ($show_time)
     534  {
     535    $temp = $date->format('H:i');
     536    if ($temp != '00:00')
     537    {
     538      $print.= ' '.$temp;
     539    }
     540  }
     541
     542  return trim($print);
     543}
     544
     545/**
     546 * Works out the time since the given date
     547 * @param: mixed, datetime string or timestamp int
     548 * @param: string, stop (year,month,week,day,hour,minute,second)
     549 * @param: string, input format
     550 * @param: bool, append text ("ago" or "in the future")
     551 * @param: bool, display weeks
     552 * @return: string
     553 */
     554function time_since($original, $stop='minute', $format=null, $with_text=true, $with_week=true)
     555{
     556  $date = str2DateTime($original, $format);
     557
     558  if (!$date)
     559  {
     560    return l10n('N/A');
     561  }
     562 
     563  $now = new DateTime();
     564  $diff = $now->diff($date);
     565 
     566  if ($with_week)
     567  {
     568    // DateInterval does not compute the number of weeks
     569    $diff->w = (int)floor($diff->d/7);
     570    $diff->d = $diff->d - $diff->w*7;
     571  }
     572  else
     573  {
     574    $diff->w = 0;
     575  }
     576 
    537577  $chunks = array(
    538     'year' => 60 * 60 * 24 * 365,
    539     'month' => 60 * 60 * 24 * 30,
    540     'week' => 60 * 60 * 24 * 7,
    541     'day' => 60 * 60 * 24,
    542     'hour' => 60 * 60,
    543     'minute' => 60,
    544     'second' => 1,
     578    'year' => 'y',
     579    'month' => 'm',
     580    'week' => 'w',
     581    'day' => 'd',
     582    'hour' => 'h',
     583    'minute' => 'i',
     584    'second' => 's',
    545585  );
    546 
    547   $today = time(); /* Current unix time  */
    548   $since = abs($today - $original);
    549 
    550   $print = null;
    551   foreach ($chunks as $name => $seconds)
    552   {
    553     if (($count = floor($since / $seconds)) != 0)
    554     {
    555       $print.= l10n_dec('%d '.$name, '%d '.$name.'s', $count);
    556       $since-= $count*$seconds;
    557     }
    558     if (!empty($print) and $chunks[$name] <= $chunks[$stop])
     586 
     587  $j = array_search($stop, array_keys($chunks));
     588 
     589  $print = ''; $i=0;
     590  foreach ($chunks as $name => $var)
     591  {
     592    if ($diff->{$var} != 0)
     593    {
     594      $print.= ' '.l10n_dec('%d '.$name, '%d '.$name.'s', $diff->{$var});
     595    }
     596    if (!empty($print) && $i >= $j)
    559597    {
    560598      break;
    561599    }
    562   }
    563 
    564   if ($today > $original)
    565   {
    566     $print = sprintf(l10n('%s ago'), $print);
    567   }
    568   else
    569   {
    570     $print = sprintf(l10n('%s in the future'), $print);
     600    $i++;
     601  }
     602 
     603  $print = trim($print);
     604 
     605  if ($with_text)
     606  {
     607    if ($diff->invert)
     608    {
     609      $print = sprintf(l10n('%s ago'), $print);
     610    }
     611    else
     612    {
     613      $print = sprintf(l10n('%s in the future'), $print);
     614    }
    571615  }
    572616
    573617  return $print;
     618}
     619
     620/**
     621 * transform a date string from a format to another (MySQL to d/M/Y for instance)
     622 * @param: string, date
     623 * @param: string, input format
     624 * @param: string, output format
     625 * @param: string, default value if inout is empty
     626 * @return: string
     627 */
     628function transform_date($original, $format_in, $format_out, $default=null)
     629{
     630  if (empty($original)) return $default;
     631  $date = str2DateTime($original, $format_in);
     632  return $date->format($format_out);
    574633}
    575634
Note: See TracChangeset for help on using the changeset viewer.