access.php
8 months ago
builder.php
11 months ago
checks.php
6 months ago
colors.php
2 years ago
data-presets.php
6 months ago
date-time.php
11 months ago
debug.php
9 months ago
education.php
11 months ago
escape-sanitize.php
6 months ago
filesystem-media.php
1 year ago
form-fields.php
8 months ago
forms.php
10 months ago
list.php
1 year ago
payments.php
10 months ago
plugins.php
11 months ago
privacy.php
1 year ago
providers.php
11 months ago
utilities.php
5 months ago
date-time.php
119 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper functions to work with dates, time and timezones. |
| 4 | * |
| 5 | * @since 1.8.0 |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Return date and time formatted as expected. |
| 10 | * |
| 11 | * @since 1.6.3 |
| 12 | * |
| 13 | * @param string|int $date Date to format. |
| 14 | * @param string $format Optional. Format for the date and time. |
| 15 | * @param bool $gmt_offset Optional. GTM offset. |
| 16 | * |
| 17 | * @return string |
| 18 | */ |
| 19 | function wpforms_datetime_format( $date, $format = '', $gmt_offset = false ) { |
| 20 | |
| 21 | if ( is_numeric( $date ) ) { |
| 22 | $date = (int) $date; |
| 23 | } |
| 24 | |
| 25 | if ( is_string( $date ) ) { |
| 26 | $date = strtotime( $date ); |
| 27 | } |
| 28 | |
| 29 | if ( $gmt_offset ) { |
| 30 | $date += (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
| 31 | } |
| 32 | |
| 33 | if ( $format === '' ) { |
| 34 | return sprintf( /* translators: %1$s - formatted date, %2$s - formatted time. */ |
| 35 | __( '%1$s at %2$s', 'wpforms-lite' ), |
| 36 | date_i18n( get_option( 'date_format' ), $date ), |
| 37 | date_i18n( get_option( 'time_format' ), $date ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | return date_i18n( $format, $date ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Return date formatted as expected. |
| 46 | * |
| 47 | * @since 1.6.3 |
| 48 | * |
| 49 | * @param string|int $date Date to format. |
| 50 | * @param string $format Optional. Format for the date. |
| 51 | * @param bool $gmt_offset Optional. GTM offset. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | function wpforms_date_format( $date, $format = '', $gmt_offset = false ) { |
| 56 | |
| 57 | if ( $format === '' ) { |
| 58 | $format = (string) get_option( 'date_format', 'M j, Y' ); |
| 59 | } |
| 60 | |
| 61 | return wpforms_datetime_format( $date, $format, $gmt_offset ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Return time formatted as expected. |
| 66 | * |
| 67 | * @since 1.8.5 |
| 68 | * |
| 69 | * @param string|int $date Date to format. |
| 70 | * @param string $format Optional. Format for the time. |
| 71 | * @param bool $gmt_offset Optional. GTM offset. |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | function wpforms_time_format( $date, $format = '', $gmt_offset = false ) { |
| 76 | |
| 77 | if ( $format === '' ) { |
| 78 | $format = (string) get_option( 'time_format', 'g:ia' ); |
| 79 | } |
| 80 | |
| 81 | return wpforms_datetime_format( $date, $format, $gmt_offset ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the certain date of a specified day in a specified format. |
| 86 | * |
| 87 | * @since 1.4.4 |
| 88 | * @since 1.6.3 Added $use_gmt_offset parameter. |
| 89 | * |
| 90 | * @param string $period Supported values: start, end. |
| 91 | * @param string $timestamp Default is the current timestamp, if left empty. |
| 92 | * @param string $format Default is a MySQL format. |
| 93 | * @param bool $use_gmt_offset Use GTM offset. |
| 94 | * |
| 95 | * @return string |
| 96 | */ |
| 97 | function wpforms_get_day_period_date( $period, $timestamp = '', $format = 'Y-m-d H:i:s', $use_gmt_offset = false ) { |
| 98 | |
| 99 | $date = ''; |
| 100 | |
| 101 | if ( empty( $timestamp ) ) { |
| 102 | $timestamp = time(); |
| 103 | } |
| 104 | |
| 105 | $offset_sec = $use_gmt_offset ? get_option( 'gmt_offset' ) * 3600 : 0; |
| 106 | |
| 107 | switch ( $period ) { |
| 108 | case 'start_of_day': |
| 109 | $date = gmdate( $format, strtotime( 'today', $timestamp ) - $offset_sec ); |
| 110 | break; |
| 111 | |
| 112 | case 'end_of_day': |
| 113 | $date = gmdate( $format, strtotime( 'tomorrow', $timestamp ) - 1 - $offset_sec ); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | return $date; |
| 118 | } |
| 119 |