1public function convert_time_to_days($date) {
2 $current_date = date("Y-m-d H:m:s");
3 $time = array();
4 $day = floor((strtotime($current_date) - strtotime($date)) / (60 * 60 * 24));
5
6 if ($day == 0) {
7 $hour = floor((strtotime($current_date) - strtotime($date)) / (60 * 60));
8
9 if ($hour == 0) {
10 $minute = floor((strtotime($current_date) - strtotime($date)) / (60));
11 $time = $minute . __d('course', "minutes ago");
12
13 } else {
14 $time = $hour . __d('course', "hours ago");
15 }
16
17 } else {
18 $time = $day . __d('course', "days ago");
19 }
20
21 return $time;
22 }
23
1<!DOCTYPE html>
2<html>
3<body>
4
5<?php
6
7function time_elapsed_string($datetime, $full = false) {
8 $now = new DateTime;
9 $ago = new DateTime($datetime);
10 $diff = $now->diff($ago);
11
12 $diff->w = floor($diff->d / 7);
13 $diff->d -= $diff->w * 7;
14
15 $string = array(
16 'y' => 'year',
17 'm' => 'month',
18 'w' => 'week',
19 'd' => 'day',
20 'h' => 'hour',
21 'i' => 'minute',
22 's' => 'second',
23 );
24 foreach ($string as $k => &$v) {
25 if ($diff->$k) {
26 $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
27 } else {
28 unset($string[$k]);
29 }
30 }
31
32 if (!$full) $string = array_slice($string, 0, 1);
33 return $string ? implode(', ', $string) . ' ago' : 'just now';
34}
35
36echo time_elapsed_string(date("Y-m-d H:i:s", 1621173863));
37?>
38
39</body>
40</html>
41
42
43 echo time_elapsed_string('2013-05-01 00:22:35');
44echo time_elapsed_string('@1367367755'); # timestamp input
45echo time_elapsed_string('2013-05-01 00:22:35', true);
46
47
48Output :
49
504 months ago
514 months ago
524 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago