|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the SVN-Buddy library. |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
|
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Helper; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Helper\Helper; |
|
15
|
|
|
|
|
16
|
|
|
class DateHelper extends Helper |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
1 |
|
public function getName() |
|
23
|
|
|
{ |
|
24
|
1 |
|
return 'date'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Returns time passed between 2 given dates in "X minutes Y seconds ago" format |
|
29
|
|
|
* |
|
30
|
|
|
* @param integer $from_date From date. |
|
31
|
|
|
* @param integer $to_date To date. |
|
32
|
|
|
* @param integer $max_levels Max levels. |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
21 |
|
public function getAgoTime($from_date, $to_date = null, $max_levels = 1) |
|
37
|
|
|
{ |
|
38
|
21 |
|
$blocks = array( |
|
39
|
21 |
|
array('name' => 'year', 'amount' => 60 * 60 * 24 * 365), |
|
40
|
21 |
|
array('name' => 'month', 'amount' => 60 * 60 * 24 * 31), |
|
41
|
21 |
|
array('name' => 'week', 'amount' => 60 * 60 * 24 * 7), |
|
42
|
21 |
|
array('name' => 'day', 'amount' => 60 * 60 * 24), |
|
43
|
21 |
|
array('name' => 'hour', 'amount' => 60 * 60), |
|
44
|
21 |
|
array('name' => 'minute', 'amount' => 60), |
|
45
|
21 |
|
array('name' => 'second', 'amount' => 1), |
|
46
|
21 |
|
); |
|
47
|
|
|
|
|
48
|
|
|
// @codeCoverageIgnoreStart |
|
49
|
|
|
if ( !isset($to_date) ) { |
|
50
|
|
|
$to_date = time(); |
|
51
|
|
|
} |
|
52
|
|
|
// @codeCoverageIgnoreEnd |
|
53
|
|
|
|
|
54
|
21 |
|
$diff = abs($to_date - $from_date); |
|
55
|
|
|
|
|
56
|
21 |
|
if ( $diff == 0 ) { |
|
57
|
1 |
|
return 'now'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
20 |
|
$current_level = 1; |
|
61
|
20 |
|
$result = array(); |
|
62
|
|
|
|
|
63
|
20 |
|
foreach ( $blocks as $block ) { |
|
64
|
20 |
|
if ( $current_level > $max_levels ) { |
|
65
|
15 |
|
break; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
20 |
|
if ( $diff / $block['amount'] >= 1 ) { |
|
69
|
20 |
|
$amount = floor($diff / $block['amount']); |
|
70
|
20 |
|
$plural = $amount > 1 ? 's' : ''; |
|
71
|
|
|
|
|
72
|
20 |
|
$result[] = $amount . ' ' . $block['name'] . $plural; |
|
73
|
20 |
|
$diff -= $amount * $block['amount']; |
|
74
|
20 |
|
$current_level++; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
20 |
|
return implode(' ', $result) . ' ago'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|