Ignore:
Timestamp:
Jul 7, 2014, 11:54:15 AM (10 years ago)
Author:
mistic100
Message:

feature 2807: nicer display of "from to" dates (required changes in "format_date" function)

File:
1 edited

Legend:

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

    r28914 r28981  
    550550    return false;
    551551  }
     552 
     553  if ($original instanceof DateTime)
     554  {
     555    return $original;
     556  }
    552557
    553558  if (!empty($format) && version_compare(PHP_VERSION, '5.3.0') >= 0)// from known date format
     
    589594 *
    590595 * @param int|string timestamp or datetime string
    591  * @param bool $show_time
    592  * @param bool $show_day_name
     596 * @param array $show list of components displayed, default is ['day_name', 'day', 'month', 'year']
     597 *    THIS PARAMETER IS PLANNED TO CHANGE
    593598 * @param string $format input format respecting date() syntax
    594599 * @return string
    595600 */
    596 function format_date($original, $show_time=false, $show_day_name=true, $format=null)
     601function format_date($original, $show=null, $format=null)
    597602{
    598603  global $lang;
     
    605610  }
    606611
     612  if ($show === null)
     613  {
     614    $show = array('day_name', 'day', 'month', 'year');
     615  }
     616
     617  // TODO use IntlDateFormatter for proper i18n
     618
    607619  $print = '';
    608   if ($show_day_name)
    609   {
     620  if (in_array('day_name', $show))
    610621    $print.= $lang['day'][ $date->format('w') ].' ';
    611   }
    612 
    613   $print.= $date->format('j');
    614   $print.= ' '.$lang['month'][ $date->format('n') ];
    615   $print.= ' '.$date->format('Y');
    616 
    617   if ($show_time)
     622
     623  if (in_array('day', $show))
     624    $print.= $date->format('j').' ';
     625
     626  if (in_array('month', $show))
     627    $print.= $lang['month'][ $date->format('n') ].' ';
     628
     629  if (in_array('year', $show))
     630    $print.= $date->format('Y').' ';
     631
     632  if (in_array('time', $show))
    618633  {
    619634    $temp = $date->format('H:i');
    620635    if ($temp != '00:00')
    621636    {
    622       $print.= ' '.$temp;
     637      $print.= $temp.' ';
    623638    }
    624639  }
    625640
    626641  return trim($print);
     642}
     643
     644/**
     645 * Format a "From ... to ..." string from two dates
     646 * @param string $from
     647 * @param string $to
     648 * @param boolean $full
     649 * @return string
     650 */
     651function format_fromto($from, $to, $full=false)
     652{
     653  $from = str2DateTime($from);
     654  $to = str2DateTime($to);
     655
     656  if ($from->format('Y-m-d') == $to->format('Y-m-d'))
     657  {
     658    return format_date($from);
     659  }
     660  else
     661  {
     662    if ($full || $from->format('Y') != $to->format('Y'))
     663    {
     664      $from_str = format_date($from);
     665    }
     666    else if ($from->format('m') != $to->format('m'))
     667    {
     668      $from_str = format_date($from, array('day_name', 'day', 'month'));
     669    }
     670    else
     671    {
     672      $from_str = format_date($from, array('day_name', 'day'));
     673    }
     674    $to_str = format_date($to);
     675
     676    return l10n('from %s to %s', $from_str, $to_str);
     677  }
    627678}
    628679
Note: See TracChangeset for help on using the changeset viewer.