-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-blogordiefuzzytime.php
42 lines (33 loc) · 1.03 KB
/
class-blogordiefuzzytime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
defined( 'ABSPATH' ) || die();
class BlogOrDieFuzzyTime {
private $reference_timestamp;
public function __construct( $reference_timestamp ) {
$this->reference_timestamp = $reference_timestamp;
}
public function over_rough_period() {
$periods = array_filter( $this->elapsed_periods() );
$longest_period = array_slice( $periods, 0, 1 );
// I prefer: 'a day' over '1 days'
if ( reset( $longest_period ) === '1' ) {
$longest_period = [ substr( key( $longest_period ), 0, -1 ) => 'a' ];
}
$str = 'over ' . reset( $longest_period ) . ' ' . key( $longest_period );
return str_replace( 'a hour', 'an hour', $str );
}
private function periods() {
return [
'years',
'days',
'hours',
'minutes',
'seconds',
];
}
private function elapsed_periods() {
$now = new DateTime( current_time('mysql') );
$then = new DateTime( "@$this->reference_timestamp" );
$difference = $now->diff( $then )->format( '%y,%a,%h,%i,%s' );
return array_combine( $this->periods(), explode( ',', $difference ) );
}
}