كود عرض التواريخ ك "منذ فترة" Display dates as “time ago”


Usage

كود:
1$time = timeAgo($dateRef);


The function takes 3 parameters:
  • timestamp: Your date
  • granularity: How far you go in the precision of your date. Default is 2 (1 day, 15 hours ago)
  • format: Desired format for the date. Default is Y-m-d H:i:s
كود:
01function  timeAgo($timestamp, $granularity=2, $format='Y-m-d H:i:s'){02        $difference = time() - $timestamp;
03        if($difference < 0) return '0 seconds ago';
04        elseif($difference < 864000){
05                $periods = array('week' => 604800,
06'day' => 86400,'hr' => 3600,'min' => 60,'sec' => 1);
07                $output = '';
08                foreach($periods as $key => $value){
09                        if($difference >= $value){
10                                $time = round($difference / $value);
11                                $difference %= $value;
12                                $output .= ($output ? ' ' : '').$time.' ';
13                                $output .= (($time > 1 && $key == 'day') ? $key.'s' : $key);
14                                $granularity--;
15                        }
16                        if($granularity == 0) break;
17                }
18                return ($output ? $output : '0 seconds').' ago';
19        }
20        else return date($format, $timestamp);
21}.