Skip to content

Instantly share code, notes, and snippets.

@MikeGillihan
Created November 12, 2025 22:44
Show Gist options
  • Select an option

  • Save MikeGillihan/f01cab437baeee95938a82f47168808c to your computer and use it in GitHub Desktop.

Select an option

Save MikeGillihan/f01cab437baeee95938a82f47168808c to your computer and use it in GitHub Desktop.
cl_date shortcode
<?php
namespace CLToolkit;
use CLToolkit\Traits\Singleton;
use DateTime;
use DateTimeZone;
use Exception;
class Shortcodes {
use Singleton;
public function init() {
add_shortcode( 'cl_date', [ $this, 'cl_date_shortcode_handler' ] );
}
/**
* Shortcode to output a date
*
* @param $atts
*
* @return string
* @throws Exception
*/
public function cl_date_shortcode_handler( $atts ) {
if ( empty( $atts ) ) {
$atts = shortcode_atts( array(
'format' => 'Y',
), $atts );
}
$date = gmdate( $atts['format'] );
$tz = get_option( 'timezone_string' );
if ( ! empty( $tz ) ) {
$tzdate = new DateTime( gmdate( 'Y-m-d H:i:s' ), new DateTimeZone( 'UTC' ) );
$tzdate->setTimezone( new DateTimeZone( $tz ) );
$date = $tzdate->format( $atts['format'] );
}
return $date;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment