Welcome Guest, Not a member yet? Register   Sign In
Updated timespan() function in date helper - return an array
#1

[eluser]Tobz[/eluser]
I've made a change to the timespan() function by adding an optional third parameter to get the results as an array instead of a string.
its much easier to work with if your processing the data instead of just displaying it.

the returned values in the array are:

years
weeks
days
hours
minutes
seconds
total_months
total_weeks
total_days
total_hours
total_minutes


And here is the function in MY_date_helper.php:

Code:
<?php
/**
* Timespan
*
* Returns a span of seconds in this format:
*  10 days 14 hours 36 minutes 47 seconds
*
* @access  public
* @param integer a number of seconds
* @param integer Unix timestamp
* @param Boolean Return an array of values instead of a string
* @return Mixed
*/
if (!function_exists('timespan')) {
  function timespan($seconds = 1, $time = '', $output_array = false) {
    $CI = &get;_instance();
    $CI->lang->load('date');
    
    if (!is_numeric($seconds)) {
      $seconds = 1;
    }
    
    if (!is_numeric($time)) {
      $time = time();
    }
    
    if ($time <= $seconds) {
      $seconds = 1;
    } else {
      $seconds = $time - $seconds;
    }

    
    $str = '';
    $output = array('years'=>0, 'weeks'=>0, 'days'=>0, 'hours'=>0, 'minutes'=>0, 'seconds'=>0, 'total_months'=>0, 'total_weeks'=>0, 'total_days'=>0, 'total_hours'=>0,'total_minutes'=>0);
    $years = floor($seconds / 31536000);
    
    $output['total_months'] = floor($seconds / 2628000);
    $output['total_weeks'] = floor($seconds / 604800);
    $output['total_days'] = floor($seconds / 86400);
    $output['total_hours'] = floor($seconds / 3600);
    $output['total_minutes'] = floor($seconds / 60);
    
    if ($years > 0) {
      $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
      $output['years'] = $years;
    }
    
    $seconds -= $years * 31536000;
    $months = floor($seconds / 2628000);
    
    if ($years > 0 OR $months > 0) {
      if ($months > 0) {
        $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
        $output['months'] = $months;
      }
      
      $seconds -= $months * 2628000;
    }
    
    $weeks = floor($seconds / 604800);
    
    if ($years > 0 OR $months > 0 OR $weeks > 0) {
      if ($weeks > 0) {
        $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
        $output['weeks'] = $weeks;
      }
      
      $seconds -= $weeks * 604800;
    }
    
    $days = floor($seconds / 86400);
    
    if ($months > 0 OR $weeks > 0 OR $days > 0) {
      if ($days > 0) {
        $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
        $output['days'] = $days;
      }
      
      $seconds -= $days * 86400;
    }
    
    $hours = floor($seconds / 3600);
    
    if ($days > 0 OR $hours > 0) {
      if ($hours > 0) {
        $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
        $output['hours'] = $hours;
      }
      
      $seconds -= $hours * 3600;
    }
    
    $minutes = floor($seconds / 60);
    
    if ($days > 0 OR $hours > 0 OR $minutes > 0) {
      if ($minutes > 0) {
        $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
        $output['minutes'] = $minutes;
      }
      
      $seconds -= $minutes * 60;
    }
    
    if ($str == '') {
      $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
    }
    $output['seconds'] = $seconds;
    
    if ($output_array == TRUE) {
      return $output;
    } else {
      return substr(trim($str), 0, -1);
    }
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB