today date to ago for the date in php

Solutions on MaxInterview for today date to ago for the date in php by the best coders in the world

showing results for - "today date to ago for the date in php"
Fanny
08 Jul 2018
1echo time_elapsed_string('2013-05-01 00:22:35');
2echo time_elapsed_string('@1367367755'); # timestamp input
3echo time_elapsed_string('2013-05-01 00:22:35', true);
4
Jerónimo
10 Apr 2020
1function time_elapsed_string($datetime, $full = false) {
2    $now = new DateTime;
3    $ago = new DateTime($datetime);
4    $diff = $now->diff($ago);
5
6    $diff->w = floor($diff->d / 7);
7    $diff->d -= $diff->w * 7;
8
9    $string = array(
10        'y' => 'year',
11        'm' => 'month',
12        'w' => 'week',
13        'd' => 'day',
14        'h' => 'hour',
15        'i' => 'minute',
16        's' => 'second',
17    );
18    foreach ($string as $k => &$v) {
19        if ($diff->$k) {
20            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
21        } else {
22            unset($string[$k]);
23        }
24    }
25
26    if (!$full) $string = array_slice($string, 0, 1);
27    return $string ? implode(', ', $string) . ' ago' : 'just now';
28}
29
Simone
08 Jun 2017
1function time_elapsed_string($datetime, $full = false) {
2    $now = new DateTime;
3    $ago = new DateTime($datetime);
4    $diff = $now->diff($ago);
5
6    $diff->w = floor($diff->d / 7);
7    $diff->d -= $diff->w * 7;
8
9    $string = array(
10        'y' => 'year',
11        'm' => 'month',
12        'w' => 'week',
13        'd' => 'day',
14        'h' => 'hour',
15        'i' => 'minute',
16        's' => 'second',
17    );
18    foreach ($string as $k => &$v) {
19        if ($diff->$k) {
20            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
21        } else {
22            unset($string[$k]);
23        }
24    }
25
26    if (!$full) $string = array_slice($string, 0, 1);
27    return $string ? implode(', ', $string) . ' ago' : 'just now';
28}
29
30echo time_elapsed_string('2013-05-01 00:22:35');
31echo time_elapsed_string('@1367367755'); # timestamp input
32echo time_elapsed_string('2013-05-01 00:22:35', true);
33
34#Output
354 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
Theo
26 Jul 2019
14 months ago
24 months ago
34 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
4