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
|
|
|
use Carbon\Carbon; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* DateTimeTrait is trait class for adding methods to generate the html code for date and time display. |
18
|
|
|
* |
19
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait DateTimeTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Format a date. |
25
|
|
|
* |
26
|
|
|
* @param int|string $date |
27
|
|
|
* @param string $format |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
33 |
|
public function date($date, $format = null) |
32
|
|
|
{ |
33
|
33 |
|
$dateObject = new \DateTime($date); |
34
|
|
|
|
35
|
33 |
|
if (null === $format) { |
36
|
32 |
|
$format = app('tinyissue.settings')->getDateFormat(); |
37
|
|
|
} |
38
|
|
|
|
39
|
33 |
|
return $dateObject->format($format); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Displays the timestamp's age in human readable format. |
44
|
|
|
* |
45
|
|
|
* @param int $timestamp |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
7 |
|
public function age($timestamp) |
50
|
|
|
{ |
51
|
7 |
|
if (!$timestamp instanceof \DateTime) { |
52
|
1 |
|
$timestamp = new \DateTime($timestamp); |
53
|
|
|
} |
54
|
|
|
|
55
|
7 |
|
return Carbon::createFromTimeStamp( |
56
|
7 |
|
$timestamp->getTimestamp() |
57
|
7 |
|
)->diffForHumans(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Convert seconds into time duration format. |
62
|
|
|
* |
63
|
|
|
* @param int $seconds |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
7 |
|
public function duration($seconds) |
68
|
|
|
{ |
69
|
7 |
|
$hours = floor($seconds / 3600); |
70
|
7 |
|
$minutes = ($seconds / 60) % 60; |
71
|
7 |
|
$seconds = $seconds % 60; |
72
|
|
|
|
73
|
7 |
|
$output = ''; |
74
|
7 |
|
$separatorChar = ', '; |
75
|
7 |
|
$separator = ''; |
76
|
7 |
|
if ($hours > 0) { |
77
|
3 |
|
$output .= $hours . ' ' . trans('tinyissue.short_hours'); |
78
|
3 |
|
$separator = $separatorChar; |
79
|
|
|
} |
80
|
7 |
|
if ($minutes > 0) { |
81
|
3 |
|
$output .= $separator . $minutes . ' ' . trans('tinyissue.short_minutes'); |
82
|
3 |
|
$separator = $separatorChar; |
83
|
|
|
} |
84
|
7 |
|
if ($seconds > 0) { |
85
|
1 |
|
$output .= $separator . $seconds . ' ' . trans('tinyissue.short_seconds'); |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
return $output; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|