smarty_modifier_timeago()   B
last analyzed

Complexity

Conditions 9
Paths 32

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 24
nc 32
nop 1
dl 0
loc 32
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * Smarty plugin
4
 * @package Smarty
5
 * @subpackage plugins
6
 */
7
8
/**
9
 * Smarty date modifier plugin
10
 * Purpose:  converts unix timestamps or datetime strings to words
11
 * Type:     modifier<br>
12
 * Name:     timeago<br>
13
 * @author   AtariLegend
14
 * @param string
15
 * @return string
16
 */
17
function smarty_modifier_timeago($timestamp) {
18
    $date = new \DateTime();
19
    $now = $date->format('U');
20
    $message = "";
21
22
    if ($now-$timestamp <60) {
23
        $diff = $now-$timestamp;
24
        $message .= " $diff seconds ago";
25
    }
26
    if ($now-$timestamp >60 and $now-$timestamp <3600) {
27
        $diff = $now-$timestamp;
28
        $diff = floor($diff / 60);
29
        $message .= " $diff minutes ago";
30
    }
31
    if ($now-$timestamp >3600 and $now-$timestamp <86400) {
32
        $diff = $now-$timestamp;
33
        $diff = floor($diff / 3600);
34
        $message .= " $diff hours ago";
35
    }
36
    if ($now-$timestamp >86400 and $now-$timestamp <172800) {
37
        $diff = $now-$timestamp;
38
        $diff = floor($diff / 3600);
0 ignored issues
show
Unused Code introduced by
The assignment to $diff is dead and can be removed.
Loading history...
39
        $message .= " yesterday";
40
    }
41
    if ($now-$timestamp >172800) {
42
        $time = new \DateTime();
43
        $time->setTimestamp($timestamp);
44
        $change_date = $time->format('M d');
45
        $message .= " on $change_date";
46
    }
47
48
    return $message;
49
}
50