Date.php
2.01 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace AC\Settings\Column;
use AC\Settings;
class Date extends Settings\Column\DateTimeFormat {
private function get_diff_html_label() {
$description = __( 'The difference is returned in a human readable format.', 'codepress-admin-columns' ) . ' <br/>' .
sprintf( __( 'For example: %s.', 'codepress-admin-columns' ),
'"' . $this->format_human_time_diff( strtotime( "-1 hour" ) ) . '" '
. __( 'or', 'codepress-admin-columns' ) .
' "' . $this->format_human_time_diff( strtotime( "-2 days" ) ) . '"'
);
return $this->get_html_label( __( 'Time Difference', 'codepress-admin-columns' ), '', $description );
}
protected function get_custom_format_options() {
$options = [
'diff' => $this->get_diff_html_label(),
'wp_default' => $this->get_default_html_label( __( 'WordPress Date Format', 'codepress-admin-columns' ) ),
];
$formats = [
'j F Y',
'Y-m-d',
'm/d/Y',
'd/m/Y',
];
foreach ( $formats as $format ) {
$options[ $format ] = $this->get_html_label_from_date_format( $format );
}
return $options;
}
protected function get_wp_default_format() {
return get_option( 'date_format' );
}
/**
* @param string $date
* @param $original_value
*
* @return string
*/
public function format( $date, $original_value ) {
$timestamp = $this->get_timestamp( $date );
if ( ! $timestamp ) {
return false;
}
if ( 'diff' === $this->get_date_format() ) {
return $this->format_human_time_diff( $timestamp );
}
return parent::format( $date, $original_value );
}
/**
* @param int $timestamp Unix time stamp
*
* @return string
*/
public function format_human_time_diff( $timestamp ) {
if ( ! $timestamp ) {
return false;
}
$current_time = current_time( 'timestamp' );
$tpl = __( '%s ago' );
if ( $timestamp > $current_time ) {
$tpl = __( 'in %s', 'codepress-admin-columns' );
}
return sprintf( $tpl, human_time_diff( $timestamp, $current_time ) );
}
}