Date   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 103
rs 10
wmc 13

3 Methods

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