Completed
Push — master ( 0df9f7...8b132e )
by Mihail
02:08
created

Date::humanize()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 54
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 7.255
c 0
b 0
f 0
cc 9
eloc 25
nc 26
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ffcms\Core\Helper;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Obj;
7
8
class Date
9
{
10
    const FORMAT_TO_DAY = 'd.m.Y';
11
    const FORMAT_TO_HOUR = 'd.m.Y H:i';
12
    const FORMAT_TO_SECONDS = 'd.m.Y H:i:s';
13
14
    const FORMAT_SQL_TIMESTAMP = 'Y-m-d H:i:s';
15
    const FORMAT_SQL_DATE = 'Y-m-d';
16
17
    /**
18
     * Try to convert string to date time format
19
     * @param string|int $rawDate
20
     * @param string $format
21
     * @return string|bool
22
     */
23
    public static function convertToDatetime($rawDate, $format = 'd.m.Y')
24
    {
25
        if (Obj::isLikeInt($rawDate)) { // convert timestamp to date format
26
            $rawDate = date($format, $rawDate);
27
        }
28
        try {
29
            $object = new \DateTime($rawDate);
30
            return $object->format($format);
31
        } catch (\Exception $e) {
32
            return false;
33
        }
34
    }
35
36
    /**
37
     * Try to convert string to unix timestamp format. Return 0 if converting is failed
38
     * @param string $date
39
     * @return int
40
     */
41
    public static function convertToTimestamp($date)
42
    {
43
        if (Obj::isObject($date) || Obj::isArray($date)) {
44
            return 0;
45
        }
46
        return (int)strtotime($date);
47
    }
48
49
    /**
50
     * Humanize date format
51
     * @param string|int $raw
52
     * @return bool|string
53
     */
54
    public static function humanize($raw)
55
    {
56
        // convert to timestamp
57
        $timestamp = $raw;
58
        if (!Obj::isInt($raw)) {
59
            $timestamp = self::convertToTimestamp($timestamp);
60
        }
61
        // calculate difference between tomorrow day midnight and passed date
62
        $diff = time() - $timestamp;
63
64
        // date in future, lets return as is
65
        if ($diff < 0) {
66
            return self::convertToDatetime($timestamp, static::FORMAT_TO_SECONDS);
67
        }
68
69
        // calculate delta and make offset sub. Maybe usage instance of Datetime is better, but localization is sucks!
70
        $deltaSec = $diff % 60;
71
        $diff /= 60;
72
73
        $deltaMin = $diff % 60;
74
        $diff /= 60;
75
76
        $deltaHour = $diff % 24;
77
        $diff /= 24;
78
79
        $deltaDays = ($diff > 1) ? (int)floor($diff) : (int)$diff;
80
81
        // sounds like more then 1 day's ago
82
        if ($deltaDays > 1) {
83
            if ($deltaDays > 14) { // sounds like more then 2 week ago, just return as is
84
                return self::convertToDatetime($timestamp, static::FORMAT_TO_HOUR);
85
            }
86
87
            return App::$Translate->get('DateHuman', '%days% days ago', ['days' => (int)$deltaDays]);
88
        }
89
90
        // sounds like yesterday
91
        if ($deltaDays === 1) {
92
            return App::$Translate->get('DateHuman', 'Yestarday, %hi%', ['hi' => self::convertToDatetime($timestamp, 'H:i')]);
93
        }
94
95
        // sounds like today, more then 1 hour ago
96
        if ($deltaHour >= 1) {
97
            return App::$Translate->get('DateHuman', '%h% hours ago', ['h' => $deltaHour]);
98
        }
99
100
        // sounds like last hour ago
101
        if ($deltaMin >= 1) {
102
            return App::$Translate->get('DateHuman', '%m% minutes ago', ['m' => $deltaMin]);
103
        }
104
105
        // just few seconds left, lets return it
106
        return App::$Translate->get('DateHuman', '%s% seconds ago', ['s' => $deltaSec]);
107
    }
108
}