2012-08-22 82 views
0

我使用Codeigniter,它有timespan()函數,返回時間爲1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes如果時間少於24小時前顯示x時間前

如果時間在過去24小時內,我想要做的只是顯示格式化的時間,否則只顯示正常的日期時間。

我覺得必須有一個功能已經做到這一點,但我一直沒有找到它的運氣。

這是Codeigniter附帶的時間跨度函數,我該如何改變它?

/** 
* 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 
* @return integer 
*/ 
if (! function_exists('timespan')) 
{ 
    function timespan($seconds = 1, $time = '') 
    { 
     $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 = ''; 
     $years = floor($seconds/31536000); 

     if ($years > 0) 
     { 
      $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', '; 
     } 

     $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')).', '; 
      } 

      $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')).', '; 
      } 

      $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')).', '; 
      } 

      $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')).', '; 
      } 

      $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')).', '; 
      } 

      $seconds -= $minutes * 60; 
     } 

     if ($str == '') 
     { 
      $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', '; 
     } 

     return substr(trim($str), 0, -1); 
    } 
} 
+0

需要moar jQuery –

回答

2

此函數將接受字符串,數字(unix)時間戳或DateTime對象。它也接受jQuery.now()。時間可能在未來或過去。

function time_ago($time=false, $just_now=false) { 
    if ($time instanceOf DateTime) 
     $time = $time->getTimestamp(); 
    elseif (is_numeric($time)) 
     $time = date('m/d/y h:i A', $time); 
    if (strtotime($time) === false) 
     $time = date('m/d/y h:i A', time()); 
    $interval = date_create($time)->diff(date_create('now')); 
    $adjective = strtotime($time) > time() ? 'from now' : 'ago'; 
    return (
     $interval->days > 0 ? 
      $time : (
       $interval->h < 1 && $interval->i < 1 && $just_now ? 
        'just now' : 
        (
         $interval->h > 1 ? 
          $interval->h.' hour'.(
           $interval->h > 1 ? 
            's' : 
            '' 
          ).' ago' : 
          $interval->i.' minutes'.' '.$adjective 
        ) 
      ) 
    ); 
} 


echo time_ago('8/22/2012 5:00 PM'); // 3 hours ago 
echo time_ago('8/21/2012 5:00 PM'); // 8/21/2012 5:00 PM 
echo time_ago(time()); // 0 hours ago 
echo time_ago(time(), true); // just now 
echo time_ago(strtotime('5 days ago')); // 08/17/12 08:18 PM 
echo time_ago(strtotime('5 hours ago')); // 5 hours ago 
echo time_ago(strtotime('5 minutes ago')); // 5 minutes ago 
echo time_ago(strtotime('+5 minutes')); // 5 minutes from now 

echo time_ago('jQuery.now()', true); // just now 
echo time_ago('sweet explosions, bro!', true); // just now 

文檔

+1

這是正確答案,因爲它包含jQuery –

+1

@Chris pfft,你的回答不接受「$ .now();」 – Lusitanian

+0

@Lusitanian我的一個嚴重的,糟糕的實踐遺漏,我糾正了它。感謝您幫助我成爲更好的程序員。 –

2
if(time() - $yourTime <= 86400) { // 86400 seconds in a day 
    echo timespan($yourTime); 
} else { 
    echo date('m/d/Y \a\t H:i:s', $yourTime); 
} 

不要依賴於一個框架,尤其是一個壞的,一切!

+0

當然除了jQuery當然 –

+0

我的回答需要MOAR jQuery! – Lusitanian

1

擺弄$格式以滿足您的需求。將接受幾乎任何東西作爲輸入。

<?php 

/** 
* RelativeTime - pretty printed 
* @author Dejan Marjanovic 
*/ 
class Site5_RelativeTime 
{ 

    private $interval = ''; 

    public function __construct() 
    { 
     call_user_func_array(array($this, 'calculate'), func_get_args()); 
    } 

    public function calculate($start, $end = NULL) 
    { 

     if (empty($start)) 
      return false; 

     if (empty($end)) 
      $end = time(); 

     if (! is_numeric($start)) 
      $start = strtotime($start); 

     if (! is_numeric($end)) 
      $end = strtotime($end);   

     if($start > $end) 
      $future = TRUE; 

     $start = '@' . $start; 
     $end = '@' . $end; 

     if (! ($start instanceof DateTime)) 
      $start = new DateTime($start); 

     if ($end === null) 
      $end = new DateTime(); 

     if (! ($end instanceof DateTime)) 
      $end = new DateTime($end); 

     $interval = $end->diff($start); 

     $get_plural = function($int, $str) 
     { 
      return $int > 1? $str.'s': $str; 
     }; 

     $format = array(); 

     if ($interval->y !== 0) 
      $format[] = "%y " . $get_plural($interval->y, "year"); 
     if ($interval->m !== 0) 
      $format[] = "%m " . $get_plural($interval->m, "month"); 
     if ($interval->d !== 0) 
      $format[] = "%d " . $get_plural($interval->d, "day"); 
     if ($interval->h !== 0) 
      $format[] = "%h " . $get_plural($interval->h, "hour"); 
     if ($interval->i !== 0) 
      $format[] = "%i " . $get_plural($interval->i, "minute"); 


     if ($interval->s !== 0) 
     { 
      if (! count($format)) 
      { 
       $this->interval = "less than a minute"; 
       return; 
      } 
      else 
      { 
       $format[] = "%s " . $get_plural($interval->s, "second"); 
      } 
     } 

     if (count($format) > 1) 
     { 
      $format = array_shift($format) . " and " . array_shift($format); 
     } 
     else 
     { 
      $format = array_pop($format); 
     } 

     $tense = ($future === TRUE)? 'from now': 'ago'; 

     $this->interval = $interval->format($format) . ' ' . $tense; 
    } 

    public function __toString() 
    { 
     return $this->interval; 
    } 

} 
+0

需要moar jquery – Lusitanian