| 1 |
<?php |
| 2 |
/** |
| 3 |
* UsersWP date related functions |
| 4 |
* |
| 5 |
* All UsersWP date related functions can be found here. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @author GeoDirectory Team <info@wpgeodirectory.com> |
| 9 |
*/ |
| 10 |
class UsersWP_Date { |
| 11 |
|
| 12 |
/** |
| 13 |
* Converts PHP Date format to jQuery UI date format. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* @package userswp |
| 17 |
* @param string $php_format Date Format. |
| 18 |
* @return string Formatted Date. |
| 19 |
*/ |
| 20 |
public function date_format_php_to_jqueryui( $php_format ) { |
| 21 |
$symbols = array( |
| 22 |
// Day |
| 23 |
'd' => 'dd', |
| 24 |
'D' => 'D', |
| 25 |
'j' => 'd', |
| 26 |
'l' => 'DD', |
| 27 |
'N' => '', |
| 28 |
'S' => '', |
| 29 |
'w' => '', |
| 30 |
'z' => 'o', |
| 31 |
// Week |
| 32 |
'W' => '', |
| 33 |
// Month |
| 34 |
'F' => 'MM', |
| 35 |
'm' => 'mm', |
| 36 |
'M' => 'M', |
| 37 |
'n' => 'm', |
| 38 |
't' => '', |
| 39 |
// Year |
| 40 |
'L' => '', |
| 41 |
'o' => '', |
| 42 |
'Y' => 'yy', |
| 43 |
'y' => 'y', |
| 44 |
// Time |
| 45 |
'a' => 'tt', |
| 46 |
'A' => 'TT', |
| 47 |
'B' => '', |
| 48 |
'g' => 'h', |
| 49 |
'G' => 'H', |
| 50 |
'h' => 'hh', |
| 51 |
'H' => 'HH', |
| 52 |
'i' => 'mm', |
| 53 |
's' => '', |
| 54 |
'u' => '' |
| 55 |
); |
| 56 |
|
| 57 |
$jqueryui_format = ""; |
| 58 |
$escaping = false; |
| 59 |
|
| 60 |
for ( $i = 0; $i < strlen( $php_format ); $i++ ) { |
| 61 |
$char = $php_format[$i]; |
| 62 |
|
| 63 |
// PHP date format escaping character |
| 64 |
if ( $char === '\\' ) { |
| 65 |
$i++; |
| 66 |
|
| 67 |
if ( $escaping ) { |
| 68 |
$jqueryui_format .= $php_format[$i]; |
| 69 |
} else { |
| 70 |
$jqueryui_format .= '\'' . $php_format[$i]; |
| 71 |
} |
| 72 |
|
| 73 |
$escaping = true; |
| 74 |
} else { |
| 75 |
if ( $escaping ) { |
| 76 |
$jqueryui_format .= "'"; |
| 77 |
$escaping = false; |
| 78 |
} |
| 79 |
|
| 80 |
if ( isset( $symbols[$char] ) ) { |
| 81 |
$jqueryui_format .= $symbols[$char]; |
| 82 |
} else { |
| 83 |
$jqueryui_format .= $char; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return $jqueryui_format; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Converts date from one format to another. |
| 93 |
* |
| 94 |
* @since 1.0.0 |
| 95 |
* @package userswp |
| 96 |
* @param string $date_input Date string to convert. |
| 97 |
* @param string $date_to Date format to convert to. |
| 98 |
* @param string $date_from Date format to convert from. |
| 99 |
* @return string Converted date. |
| 100 |
*/ |
| 101 |
public function date($date_input, $date_to, $date_from = '') { |
| 102 |
if (empty($date_input) || empty($date_to)) { |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
$date = ''; |
| 107 |
if (!empty($date_from)) { |
| 108 |
$datetime = date_create_from_format($date_from, $date_input); |
| 109 |
|
| 110 |
if (!empty($datetime)) { |
| 111 |
$date = $datetime->format($date_to); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (empty($date)) { |
| 116 |
$date = strpos($date_input, '/') !== false ? str_replace('/', '-', $date_input) : $date_input; |
| 117 |
$date = date_i18n($date_to, strtotime($date)); |
| 118 |
} |
| 119 |
|
| 120 |
$date = $this->maybe_untranslate_date($date); |
| 121 |
|
| 122 |
return apply_filters('uwp_date', $date, $date_input, $date_to, $date_from); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Converts non english date months to english date months. |
| 127 |
* |
| 128 |
* @since 1.0.0 |
| 129 |
* @package userswp |
| 130 |
* @param string $date Date String. |
| 131 |
* @return string Converted Date. |
| 132 |
*/ |
| 133 |
public function maybe_untranslate_date($date){ |
| 134 |
$english_long_months = array( |
| 135 |
'January', |
| 136 |
'February', |
| 137 |
'March', |
| 138 |
'April', |
| 139 |
'May', |
| 140 |
'June', |
| 141 |
'July', |
| 142 |
'August', |
| 143 |
'September', |
| 144 |
'October', |
| 145 |
'November', |
| 146 |
'December', |
| 147 |
); |
| 148 |
|
| 149 |
$non_english_long_months = array( |
| 150 |
__('January'), |
| 151 |
__('February'), |
| 152 |
__('March'), |
| 153 |
__('April'), |
| 154 |
__('May'), |
| 155 |
__('June'), |
| 156 |
__('July'), |
| 157 |
__('August'), |
| 158 |
__('September'), |
| 159 |
__('October'), |
| 160 |
__('November'), |
| 161 |
__('December'), |
| 162 |
); |
| 163 |
$date = str_replace($non_english_long_months,$english_long_months,$date); |
| 164 |
|
| 165 |
|
| 166 |
$english_short_months = array( |
| 167 |
' Jan ', |
| 168 |
' Feb ', |
| 169 |
' Mar ', |
| 170 |
' Apr ', |
| 171 |
' May ', |
| 172 |
' Jun ', |
| 173 |
' Jul ', |
| 174 |
' Aug ', |
| 175 |
' Sep ', |
| 176 |
' Oct ', |
| 177 |
' Nov ', |
| 178 |
' Dec ', |
| 179 |
); |
| 180 |
|
| 181 |
$non_english_short_months = array( |
| 182 |
' '._x( 'Jan', 'January abbreviation' ).' ', |
| 183 |
' '._x( 'Feb', 'February abbreviation' ).' ', |
| 184 |
' '._x( 'Mar', 'March abbreviation' ).' ', |
| 185 |
' '._x( 'Apr', 'April abbreviation' ).' ', |
| 186 |
' '._x( 'May', 'May abbreviation' ).' ', |
| 187 |
' '._x( 'Jun', 'June abbreviation' ).' ', |
| 188 |
' '._x( 'Jul', 'July abbreviation' ).' ', |
| 189 |
' '._x( 'Aug', 'August abbreviation' ).' ', |
| 190 |
' '._x( 'Sep', 'September abbreviation' ).' ', |
| 191 |
' '._x( 'Oct', 'October abbreviation' ).' ', |
| 192 |
' '._x( 'Nov', 'November abbreviation' ).' ', |
| 193 |
' '._x( 'Dec', 'December abbreviation' ).' ', |
| 194 |
); |
| 195 |
|
| 196 |
$date = str_replace($non_english_short_months,$english_short_months,$date); |
| 197 |
|
| 198 |
|
| 199 |
return $date; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Gets random date. |
| 204 |
* |
| 205 |
* @since 1.0.0 |
| 206 |
* @package userswp |
| 207 |
* |
| 208 |
* @param int $days_from Random days from. |
| 209 |
* @param int $days_to Random days to. |
| 210 |
* |
| 211 |
* @return string Formatted date string. |
| 212 |
*/ |
| 213 |
public function get_random_date( $days_from = 30, $days_to = 0 ) { |
| 214 |
// 1 day in seconds is 86400 |
| 215 |
$from = $days_from * rand( 10000, 99999 ); |
| 216 |
|
| 217 |
// $days_from should always be less than $days_to |
| 218 |
if ( $days_to > $days_from ) { |
| 219 |
$days_to = $days_from - 1; |
| 220 |
} |
| 221 |
|
| 222 |
$to = $days_to * rand( 10000, 99999 ); |
| 223 |
$date_from = time() - $from; |
| 224 |
$date_to = time() - $to; |
| 225 |
|
| 226 |
return date( 'Y-m-d H:i:s', rand( $date_from, $date_to ) ); |
| 227 |
} |
| 228 |
|
| 229 |
} |