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