Completed
Push — master ( 50f309...1f3d4e )
by Mohamed
06:23
created

DateTimeTrait::date()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 2
b 1
f 0
nc 2
nop 2
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Extensions\Html\Traits;
13
14
/**
15
 * DateTimeTrait is trait class for adding methods to generate the html code for date and time display.
16
 *
17
 * @author Mohamed Alsharaf <[email protected]>
18
 */
19
trait DateTimeTrait
20
{
21
    /**
22
     * Format a date.
23
     *
24
     * @param int|string $date
25
     * @param string     $format
26
     *
27
     * @return string
28
     */
29 29
    public function date($date, $format = null)
30
    {
31 29
        $dateObject = new \DateTime($date);
32
33 29
        if (null === $format) {
34 29
            $format = app('tinyissue.settings')->getDateFormat();
35
        }
36
37 29
        return $dateObject->format($format);
38
    }
39
40
    /**
41
     * Displays the timestamp's age in human readable format.
42
     *
43
     * @param int $timestamp
44
     *
45
     * @return string
46
     */
47 6
    public function age($timestamp)
48
    {
49 6
        if (!$timestamp instanceof \DateTime) {
50
            $timestamp = new \DateTime($timestamp);
51
        }
52
53 6
        $timestamp  = $timestamp->getTimestamp();
54 6
        $difference = time() - $timestamp;
55 6
        $periods    = ['second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade'];
56 6
        $lengths    = ['60', '60', '24', '7', '4.35', '12', '10'];
57 6
        for ($j = 0; $difference >= $lengths[$j]; ++$j) {
58
            $difference /= $lengths[$j];
59
        }
60 6
        $difference = round($difference);
61 6
        if ($difference != 1) {
62 4
            $periods[$j] .= 's';
63
        }
64
65 6
        return $difference . ' ' . $periods[$j] . ' ago';
66
    }
67
68
    /**
69
     * Convert seconds into time duration format.
70
     *
71
     * @param int $seconds
72
     *
73
     * @return string
74
     */
75 6
    public function duration($seconds)
76
    {
77 6
        $hours   = floor($seconds / 3600);
78 6
        $minutes = ($seconds / 60) % 60;
79 6
        $seconds = $seconds % 60;
80
81 6
        $output        = '';
82 6
        $separatorChar = ', ';
83 6
        $separator     = '';
84 6
        if ($hours > 0) {
85 3
            $output .= $hours . ' ' . trans('tinyissue.short_hours');
86 3
            $separator = $separatorChar;
87
        }
88 6
        if ($minutes > 0) {
89 3
            $output .= $separator . $minutes . ' ' . trans('tinyissue.short_minutes');
90 3
            $separator = $separatorChar;
91
        }
92 6
        if ($seconds > 0) {
93 3
            $output .= $separator . $seconds . ' ' . trans('tinyissue.short_seconds');
94
        }
95
96 6
        return $output;
97
    }
98
}
99