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
|
21 |
|
public function date($date, $format = 'F jS \a\t g:i A') |
30
|
|
|
{ |
31
|
21 |
|
$dateObject = new \DateTime($date); |
32
|
|
|
|
33
|
21 |
|
return $dateObject->format($format); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Displays the timestamp's age in human readable format |
38
|
|
|
* |
39
|
|
|
* @param int $timestamp |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
3 |
|
public function age($timestamp) |
44
|
|
|
{ |
45
|
3 |
|
if (!$timestamp instanceof \DateTime) { |
46
|
|
|
$timestamp = new \DateTime($timestamp); |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
$timestamp = $timestamp->getTimestamp(); |
50
|
3 |
|
$difference = time() - $timestamp; |
51
|
3 |
|
$periods = ['second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade']; |
52
|
3 |
|
$lengths = ['60', '60', '24', '7', '4.35', '12', '10']; |
53
|
3 |
|
for ($j = 0; $difference >= $lengths[$j]; ++$j) { |
54
|
|
|
$difference /= $lengths[$j]; |
55
|
|
|
} |
56
|
3 |
|
$difference = round($difference); |
57
|
3 |
|
if ($difference != 1) { |
58
|
3 |
|
$periods[$j] .= 's'; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
return $difference . ' ' . $periods[$j] . ' ago'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Convert seconds into time duration format |
66
|
|
|
* |
67
|
|
|
* @param int $seconds |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
5 |
|
public function duration($seconds) |
72
|
|
|
{ |
73
|
5 |
|
$hours = floor($seconds / 3600); |
74
|
5 |
|
$minutes = ($seconds / 60) % 60; |
75
|
5 |
|
$seconds = $seconds % 60; |
76
|
|
|
|
77
|
5 |
|
$output = ''; |
78
|
5 |
|
$separatorChar = ', '; |
79
|
5 |
|
$separator = ''; |
80
|
5 |
|
if ($hours > 0) { |
81
|
2 |
|
$output .= $hours . ' ' . trans('tinyissue.short_hours'); |
82
|
2 |
|
$separator = $separatorChar; |
83
|
|
|
} |
84
|
5 |
|
if ($minutes > 0) { |
85
|
2 |
|
$output .= $separator . $minutes . ' ' . trans('tinyissue.short_minutes'); |
86
|
2 |
|
$separator = $separatorChar; |
87
|
|
|
} |
88
|
5 |
|
if ($seconds > 0) { |
89
|
2 |
|
$output .= $separator . $seconds . ' ' . trans('tinyissue.short_seconds'); |
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
return $output; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|