| | 2346 | * Returns a formatted date in the local timezone. This is a drop-in |
| | 2347 | * replacement for `date()`, except that the returned string will be formatted |
| | 2348 | * for the local timezone. |
| | 2349 | * |
| | 2350 | * If there is a timezone_string available, the date is assumed to be in that |
| | 2351 | * timezone, otherwise it simply subtracts the value of the 'gmt_offset' |
| | 2352 | * option. |
| | 2353 | * |
| | 2354 | * @uses get_option() to retrieve the value of 'gmt_offset'. |
| | 2355 | * @param string $format The format of the outputted date string. |
| | 2356 | * @param string $timestamp Optional. If absent, defaults to `time()`. |
| | 2357 | * @return string GMT version of the date provided. |
| | 2358 | */ |
| | 2359 | function wp_date( $format, $timestamp = false ) { |
| | 2360 | $tz = get_option( 'timezone_string' ); |
| | 2361 | if ( ! $timestamp ) { |
| | 2362 | $timestamp = time(); |
| | 2363 | } |
| | 2364 | if ( $tz ) { |
| | 2365 | $date = date_create( '@' . $timestamp ); |
| | 2366 | if ( ! $date ) { |
| | 2367 | return gmdate( $format, 0 ); |
| | 2368 | } |
| | 2369 | $date->setTimezone( new DateTimeZone( $tz ) ); |
| | 2370 | return $date->format( $format ); |
| | 2371 | } else { |
| | 2372 | return date( $format, $timestamp + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); |
| | 2373 | } |
| | 2374 | } |
| | 2375 | |
| | 2376 | /** |
| | 2377 | * Converts a locally-formatted date to a unix timestamp. This is a drop-in |
| | 2378 | * replacement for `strtotime()`, except that where strtotime assumes GMT, this |
| | 2379 | * assumes local time (as described below). If a timezone is specified, this |
| | 2380 | * function defers to strtotime(). |
| | 2381 | * |
| | 2382 | * If there is a timezone_string available, the date is assumed to be in that |
| | 2383 | * timezone, otherwise it simply subtracts the value of the 'gmt_offset' |
| | 2384 | * option. |
| | 2385 | * |
| | 2386 | * @see strtotime() |
| | 2387 | * @uses get_option() to retrieve the value of 'gmt_offset'. |
| | 2388 | * @param string $string A date/time string. See `strtotime` for valid formats. |
| | 2389 | * @return int UNIX timestamp. |
| | 2390 | */ |
| | 2391 | function wp_strtotime( $string ) { |
| | 2392 | // If there's a timezone specified, we shouldn't convert it |
| | 2393 | try { |
| | 2394 | $test_date = new DateTime( $string ); |
| | 2395 | if ( 'UTC' != $test_date->getTimezone()->getName() ) { |
| | 2396 | return strtotime( $string ); |
| | 2397 | } |
| | 2398 | } catch ( Exception $e ) { |
| | 2399 | return strtotime( $string ); |
| | 2400 | } |
| | 2401 | |
| | 2402 | $tz = get_option( 'timezone_string' ); |
| | 2403 | if ( $tz ) { |
| | 2404 | $date = date_create( $string, new DateTimeZone( $tz ) ); |
| | 2405 | if ( ! $date ) { |
| | 2406 | return strtotime( $string ); |
| | 2407 | } |
| | 2408 | $date->setTimezone( new DateTimeZone( 'UTC' ) ); |
| | 2409 | return $date->getTimestamp(); |
| | 2410 | } else { |
| | 2411 | return strtotime( $string ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
| | 2412 | } |
| | 2413 | } |
| | 2414 | |
| | 2415 | /** |