Arrays.php
7 years ago
Date.php
7 years ago
File.php
7 years ago
Html.php
7 years ago
Icon.php
7 years ago
Image.php
7 years ago
Media.php
7 years ago
Network.php
7 years ago
Post.php
7 years ago
Strings.php
7 years ago
Taxonomy.php
7 years ago
User.php
7 years ago
Date.php
179 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Helper; |
| 4 | |
| 5 | class Date { |
| 6 | |
| 7 | /** |
| 8 | * @param string $date |
| 9 | * |
| 10 | * @return int|false |
| 11 | */ |
| 12 | public function strtotime( $date ) { |
| 13 | if ( empty( $date ) || in_array( $date, array( '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ) ) || ! is_scalar( $date ) ) { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | // some plugins store dates in a jquery timestamp format, format is in ms since The Epoch. |
| 18 | // See http://api.jqueryui.com/datepicker/#utility-formatDate |
| 19 | if ( is_numeric( $date ) ) { |
| 20 | $length = strlen( trim( $date ) ); |
| 21 | |
| 22 | // Dates before / around September 8th, 2001 are saved as 9 numbers * 1000 resulting in 12 numbers to store the time. |
| 23 | // Dates after September 8th are saved as 10 numbers * 1000, resulting in 13 numbers. |
| 24 | // For example the ACF Date and Time Picker uses this format. |
| 25 | // credits: Ben C |
| 26 | if ( 12 === $length || 13 === $length ) { |
| 27 | $date = round( $date / 1000 ); // remove the ms |
| 28 | } |
| 29 | |
| 30 | // Date format: yyyymmdd ( often used by ACF ) must start with 19xx or 20xx and is 8 long |
| 31 | // in theory a numeric string of 8 can also be a unix timestamp; no conversion would be needed |
| 32 | if ( 8 === $length && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) { |
| 33 | $date = strtotime( $date ); |
| 34 | } |
| 35 | } else { |
| 36 | $date = strtotime( $date ); |
| 37 | } |
| 38 | |
| 39 | return $date; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param string $date |
| 44 | * @param string $format |
| 45 | * |
| 46 | * @return int|false |
| 47 | */ |
| 48 | public function get_timestamp_from_format( $date, $format ) { |
| 49 | if ( ! $date ) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // Already a timestamp |
| 54 | if ( 'U' === $format ) { |
| 55 | return $date; |
| 56 | } |
| 57 | |
| 58 | $timestamp = false; |
| 59 | |
| 60 | // since PHP 5.3.0 |
| 61 | // Create timestamp from a specific date format |
| 62 | if ( function_exists( 'date_create_from_format' ) && $format ) { |
| 63 | if ( $date = date_create_from_format( $format, $date ) ) { |
| 64 | $timestamp = date_format( $date, 'U' ); |
| 65 | } |
| 66 | } // before PHP 5.3.0 |
| 67 | else { |
| 68 | $timestamp = $this->strtotime( $date ); |
| 69 | } |
| 70 | |
| 71 | return $timestamp; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 1.3.1 |
| 76 | * |
| 77 | * @param string $date PHP Date format |
| 78 | * @param string $display_format Date display format |
| 79 | * |
| 80 | * @return string Formatted date |
| 81 | */ |
| 82 | public function date( $date, $display_format = '' ) { |
| 83 | $timestamp = ac_helper()->date->strtotime( $date ); |
| 84 | |
| 85 | return $this->date_by_timestamp( $timestamp, $display_format ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @since 3.0 |
| 90 | * |
| 91 | * @param $timestamp |
| 92 | * @param string $display_format Date display format |
| 93 | * |
| 94 | * @return string Formatted date |
| 95 | */ |
| 96 | public function date_by_timestamp( $timestamp, $display_format = '' ) { |
| 97 | if ( ! $timestamp ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | switch ( $display_format ) { |
| 102 | |
| 103 | case 'wp_date' : |
| 104 | $display_format = get_option( 'date_format' ); |
| 105 | |
| 106 | break; |
| 107 | case 'wp_date_time' : |
| 108 | $display_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
| 109 | |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | // Get date format from the General Settings |
| 114 | if ( ! $display_format ) { |
| 115 | $display_format = get_option( 'date_format' ); |
| 116 | } |
| 117 | |
| 118 | // Fallback in case the date format from General Settings is empty |
| 119 | if ( ! $display_format ) { |
| 120 | $display_format = 'F j, Y'; |
| 121 | } |
| 122 | |
| 123 | return date_i18n( $display_format, $timestamp ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @since 1.3.1 |
| 128 | * |
| 129 | * @param string $date |
| 130 | * @param string $format |
| 131 | * |
| 132 | * @return string Formatted time |
| 133 | */ |
| 134 | public function time( $date, $format = '' ) { |
| 135 | $timestamp = ac_helper()->date->strtotime( $date ); |
| 136 | |
| 137 | if ( ! $format ) { |
| 138 | $format = get_option( 'time_format' ); |
| 139 | } |
| 140 | |
| 141 | if ( ! $timestamp ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return date_i18n( $format, $timestamp ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Translate a jQuery date format to the PHP date format |
| 150 | * @since 1.1 |
| 151 | * |
| 152 | * @param string $format jQuery date format |
| 153 | * |
| 154 | * @return string PHP date format |
| 155 | */ |
| 156 | public function parse_jquery_dateformat( $format ) { |
| 157 | $replace = array( |
| 158 | '^dd^d' => 'j', |
| 159 | 'dd' => 'd', |
| 160 | 'DD' => 'l', |
| 161 | 'o' => 'z', |
| 162 | 'MM' => 'F', |
| 163 | '^mm^m' => 'n', |
| 164 | 'mm' => 'm', |
| 165 | 'yy' => 'Y', |
| 166 | ); |
| 167 | |
| 168 | $replace_from = array(); |
| 169 | $replace_to = array(); |
| 170 | |
| 171 | foreach ( $replace as $from => $to ) { |
| 172 | $replace_from[] = '/' . $from . '/'; |
| 173 | $replace_to[] = $to; |
| 174 | } |
| 175 | |
| 176 | return preg_replace( $replace_from, $replace_to, $format ); |
| 177 | } |
| 178 | |
| 179 | } |