* Helper functions to work with dates, time and timezones.
* Return date and time formatted as expected.
* @param string|int $date Date to format.
* @param string $format Optional. Format for the date and time.
* @param bool $gmt_offset Optional. GTM offset.
function wpforms_datetime_format( $date, $format = '', $gmt_offset = false ) {
if ( is_numeric( $date ) ) {
if ( is_string( $date ) ) {
$date = strtotime( $date );
$date += (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
return sprintf( /* translators: %1$s - formatted date, %2$s - formatted time. */
__( '%1$s at %2$s', 'wpforms-lite' ),
date_i18n( get_option( 'date_format' ), $date ),
date_i18n( get_option( 'time_format' ), $date )
return date_i18n( $format, $date );
* Return date formatted as expected.
* @param string|int $date Date to format.
* @param string $format Optional. Format for the date.
* @param bool $gmt_offset Optional. GTM offset.
function wpforms_date_format( $date, $format = '', $gmt_offset = false ) {
$format = (string) get_option( 'date_format', 'M j, Y' );
return wpforms_datetime_format( $date, $format, $gmt_offset );
* Return time formatted as expected.
* @param string|int $date Date to format.
* @param string $format Optional. Format for the time.
* @param bool $gmt_offset Optional. GTM offset.
function wpforms_time_format( $date, $format = '', $gmt_offset = false ) {
$format = (string) get_option( 'time_format', 'g:ia' );
return wpforms_datetime_format( $date, $format, $gmt_offset );
* Get the certain date of a specified day in a specified format.
* @since 1.6.3 Added $use_gmt_offset parameter.
* @param string $period Supported values: start, end.
* @param string $timestamp Default is the current timestamp, if left empty.
* @param string $format Default is a MySQL format.
* @param bool $use_gmt_offset Use GTM offset.
function wpforms_get_day_period_date( $period, $timestamp = '', $format = 'Y-m-d H:i:s', $use_gmt_offset = false ) {
if ( empty( $timestamp ) ) {
$offset_sec = $use_gmt_offset ? get_option( 'gmt_offset' ) * 3600 : 0;
$date = gmdate( $format, strtotime( 'today', $timestamp ) - $offset_sec );
$date = gmdate( $format, strtotime( 'tomorrow', $timestamp ) - 1 - $offset_sec );