class-foo-friendly-dates.php
4 weeks ago
class-foo-plugin-base.php
4 weeks ago
class-foo-plugin-file-locator.php
4 weeks ago
class-foo-plugin-metabox-sanity.php
4 weeks ago
class-foo-plugin-options.php
4 weeks ago
class-foo-plugin-settings.php
4 weeks ago
class-foo-plugin-textdomain.php
4 weeks ago
index.php
4 weeks ago
class-foo-friendly-dates.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /* |
| 8 | |
| 9 | Translatable Friendly Dates for WordPress |
| 10 | Written as part of Foo Plugin Base plugin framework |
| 11 | Original concept taken from Invent Partners - http://www.inventpartners.com |
| 12 | |
| 13 | Example : |
| 14 | |
| 15 | $instance = new Foo_Friendly_Dates( 'plugin_text_domain' ); |
| 16 | echo $instance->friendly_date( '22 May 2030' ); |
| 17 | echo $instance->friendly_date( 1403533149 ); |
| 18 | |
| 19 | */ |
| 20 | if ( !class_exists( 'Foo_Friendly_Dates_v1' ) ) { |
| 21 | |
| 22 | if ( !function_exists( '__' ) ) { |
| 23 | function __( $text, $domain = '' ) { |
| 24 | return $text; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | class Foo_Friendly_Dates_v1 { |
| 29 | |
| 30 | private $current_timestamp; |
| 31 | private $current_timestamp_day; |
| 32 | private $text_domain; |
| 33 | |
| 34 | const DAY = 86400; //60 * 60 * 24 |
| 35 | const WEEK = 604800; //60 * 60 * 24 * 7 |
| 36 | |
| 37 | public function __construct( $text_domain = '' ) { |
| 38 | $this->text_domain = $text_domain; |
| 39 | |
| 40 | $this->set_compare_timestamp( time() ); |
| 41 | |
| 42 | // $this->event_timestamp = $event_timestamp; |
| 43 | // $this->event_timestamp_day = mktime( 0, 0, 0, $month = date( "n", $event_timestamp ), $day = date( "j", $event_timestamp ), date( "Y", $event_timestamp ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Allow the default compare timestamp to be overridden |
| 48 | * @param $compare_timestamp |
| 49 | */ |
| 50 | public function set_compare_timestamp( $compare_timestamp ) { |
| 51 | $this->current_timestamp = $compare_timestamp; |
| 52 | $this->current_timestamp_day = mktime( 0, 0, 0, date( 'n', $compare_timestamp ), date( 'j', $compare_timestamp ), date( 'Y', $compare_timestamp ) ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Return a friendly date compared to the current date |
| 57 | * |
| 58 | * @param $timestamp string|int The timestamp we want return a friendly date for |
| 59 | * |
| 60 | * @return string A friendly date |
| 61 | */ |
| 62 | public function friendly_date( $timestamp ) { |
| 63 | |
| 64 | //check for empty, zero or null |
| 65 | if ( empty( $timestamp ) ) return ''; |
| 66 | |
| 67 | $event_timestamp = is_string( $timestamp ) ? strtotime( $timestamp ) : $timestamp; |
| 68 | $event_timestamp_day = mktime( 0, 0, 0, date( "n", $event_timestamp ), date( "j", $event_timestamp ), date( "Y", $event_timestamp ) ); |
| 69 | |
| 70 | //check for today |
| 71 | if ( $event_timestamp_day === $this->current_timestamp_day ) { |
| 72 | return 'today'; |
| 73 | } |
| 74 | |
| 75 | $diff = $event_timestamp_day - $this->current_timestamp_day; |
| 76 | |
| 77 | // Future events |
| 78 | if ( $diff > 0 ) { |
| 79 | // Tomorrow |
| 80 | if ( $diff >= self::DAY && $diff < (self::DAY * 2) ) { |
| 81 | return 'tomorrow'; |
| 82 | } |
| 83 | |
| 84 | //calculate days |
| 85 | $days = intval( $diff / self::DAY ); |
| 86 | |
| 87 | //less then a week, return number of days |
| 88 | if ( $days < 7 ) { |
| 89 | return "in $days days"; |
| 90 | } |
| 91 | |
| 92 | //calculate weeks |
| 93 | $weeks = intval( $diff / self::WEEK ); |
| 94 | |
| 95 | if ( $weeks == 1 ) { |
| 96 | return 'next week'; |
| 97 | } else if ( $weeks < 4 ) { |
| 98 | return "in $weeks weeks"; |
| 99 | } |
| 100 | |
| 101 | //calculate the event month |
| 102 | $event_month = intval( (date( 'Y', $event_timestamp_day ) * 12) + date( 'm', $event_timestamp_day ) ); |
| 103 | |
| 104 | //calculate the current month |
| 105 | $current_month = intval( (date( 'Y', $this->current_timestamp_day ) * 12) + date( 'm', $this->current_timestamp_day ) ); |
| 106 | |
| 107 | //calculate month difference |
| 108 | $months = abs( $event_month - $current_month ); |
| 109 | |
| 110 | if ( $months == 0 ) { |
| 111 | return "in a month"; |
| 112 | } else if ( $months == 1 ) { |
| 113 | return "in over a month"; |
| 114 | } else if ( $months < 12 ) { |
| 115 | return "in $months months"; |
| 116 | } |
| 117 | |
| 118 | //calculate years |
| 119 | $years = $months / 12; |
| 120 | |
| 121 | if ( $years == 1 ) { |
| 122 | return "in a year"; |
| 123 | } else if ( $years < 2 ) { |
| 124 | return "in over a year"; |
| 125 | } |
| 126 | |
| 127 | return "in $years years"; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | return 'unknown'; |
| 132 | } |
| 133 | |
| 134 | protected function calcMonthDiff() { |
| 135 | |
| 136 | $event_month = intval( (date( 'Y', $this->event_timestamp_day ) * 12) + date( 'm', $this->event_timestamp_day ) ); |
| 137 | $current_month = intval( (date( 'Y', $this->current_timestamp_day ) * 12) + date( 'm', $this->current_timestamp_day ) ); |
| 138 | $month_diff = abs( $event_month - $current_month ); |
| 139 | |
| 140 | return $month_diff; |
| 141 | |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 |