Last active
October 13, 2023 01:09
-
-
Save Crocoblock/82e399a5d11bb84f2fd4702347ea5dcc to your computer and use it in GitHub Desktop.
JetEngine Format date string macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'jet-engine/register-macros', function() { | |
/** | |
* Format date string. | |
*/ | |
class Format_Date_String extends \Jet_Engine_Base_Macros { | |
public function macros_tag() { | |
return 'format_date_string'; | |
} | |
public function macros_name() { | |
return esc_html__( 'Format date string', 'jet-engine' ); | |
} | |
public function macros_args() { | |
return array( | |
'format' => array( | |
'label' => __( 'Format', 'jet-engine' ), | |
'type' => 'text', | |
'default' => 'd.m.Y', | |
), | |
'date_string' => array( | |
'label' => __( 'Date string', 'jet-engine' ), | |
'type' => 'text', | |
'default' => 'now', | |
), | |
'show_by' => array( | |
'label' => 'Show by:', | |
'type' => 'select', | |
'options' => array( | |
'utc' => 'UTC+0', | |
'server' => 'Server time' | |
), | |
'default' => 'utc', | |
), | |
); | |
} | |
public function macros_callback( $args = array() ) { | |
$format = $args['format'] ?? ''; | |
$date_string = $args['date_string'] ?? ''; | |
$show_by = $args['show_by'] ?? 'utc'; | |
if ( ! $format || ! $date_string ) { | |
return 'cannot format'; | |
} | |
if ( $format === 'datetime' ) { | |
$format = 'Y-m-d\TH:i'; | |
} | |
if ( $show_by === 'utc' ) { | |
$base_timestamp = null; | |
} else { | |
$base_timestamp = strtotime( 'now' ) + get_option( 'gmt_offset' ) * 3600; | |
} | |
$timestamp = strtotime( $date_string, $base_timestamp ); | |
$result = date( $format, $timestamp ); | |
return $result; | |
} | |
} | |
new Format_Date_String(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment