Completed
Push — master ( 71e984...d2ae2e )
by Mihail
03:24
created

Date::convertToTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
38
     * @param string $date
39
     * @return int
40
     */
41
    public static function convertToTimestamp($date)
42
    {
43
        return strtotime($date);
44
    }
45
46
    /**
47
     * Humanize date format
48
     * @param string|int $raw
49
     * @return bool|string
50
     */
51
    public static function humanize($raw)
52
    {
53
        // convert to timestamp
54
        $timestamp = self::convertToTimestamp($raw);
55
        // calculate difference between tomorrow day midnight and passed date
56
        $diff = time() - $timestamp;
57
58
        // date in future, lets return as is
59
        if ($diff < 0) {
60
            return self::convertToDatetime($timestamp, static::FORMAT_TO_SECONDS);
61
        }
62
63
        // calculate delta and make offset sub. Maybe usage instance of Datetime is better, but localization is sucks!
64
        $deltaSec = $diff % 60;
65
        $diff /= 60;
66
67
        $deltaMin = $diff % 60;
68
        $diff /= 60;
69
70
        $deltaHour = $diff % 24;
71
        $diff /= 24;
72
73
        $deltaDays = ($diff > 1) ? (int)floor($diff) : (int)$diff;
74
75
        // sounds like more then 1 day's ago
76
        if ($deltaDays > 1) {
77
            if ($deltaDays > 14) { // sounds like more then 2 week ago, just return as is
78
                return self::convertToDatetime($timestamp, static::FORMAT_TO_HOUR);
79
            }
80
81
            return App::$Translate->get('DateHuman', '%days% days ago', ['days' => (int)$deltaDays]);
82
        }
83
84
        // sounds like yesterday
85
        if ($deltaDays === 1) {
86
            return App::$Translate->get('DateHuman', 'Yestarday, %hi%', ['hi' => self::convertToDatetime($timestamp, 'H:i')]);
87
        }
88
89
        // sounds like today, more then 1 hour ago
90
        if ($deltaHour >= 1) {
91
            return App::$Translate->get('DateHuman', '%h% hours ago', ['h' => $deltaHour]);
92
        }
93
94
        // sounds like last hour ago
95
        if ($deltaMin >= 1) {
96
            return App::$Translate->get('DateHuman', '%m% minutes ago', ['m' => $deltaMin]);
97
        }
98
99
        // just few seconds left, lets return it
100
        return App::$Translate->get('DateHuman', '%s% seconds ago', ['s' => $deltaSec]);
101
    }
102
}