In this tutorial, I'll show you how you can display the current date in WordPress with your own template function or a plugin. Online magazines and news portals in particular often show today's date to indicate that the website is up to date.
Show current date in WordPress
To get the current date in WordPress it is better to use the core function date_i18n () . The abbreviation i18n stands for internationalization. So we get the date in localized form, that is, weekdays and month names are translated automatically.
A date format must be passed as the first parameter:
echo date_i18n( 'Ymd' ); // => 2019-01-10
The WordPress function basically behaves like the normal PHP date () function, but there is no feature parity. Not all format specifications are supported by WordPress.
<?php // 10.01.2019 echo date_i18n( 'dmY' ); // Jan 10, 2019 echo date_i18n( 'M d, Y' ); // Donnerstag, 10. Januar 2019 echo date_i18n( 'l, j. FY' ); ?>
The date is translated depending on the configured language of the WordPress installation. In German, January instead of January and Thursday instead of Thursday. The translation is always done by the core, no text domain is required for themes and plugins.
The date can be output in the template files of the WordPress theme, for example in the header.php or footer.php . When adapting a foreign theme, I recommend overwriting the template with a child theme and not modifying it directly.
Use the date format of the WordPress settings
Administrators can set a desired date format for the entire website under Settings → General in the WordPress backend.
It therefore makes sense to take this selected date format into account when outputting the current date in WordPress, instead of defining the format statically in the code.
The configured format for the date can be queried with get_option () :
$date_format = get_option( 'date_format' );
Then - as already known - the date is displayed with the format determined:
echo date_i18n( $date_format );
Summarized in one line:
echo date_i18n( get_option( 'date_format' ) );
Show specific date in WordPress
If you want to display a specific date instead of today's, you can pass a timestamp as a second argument. The timestamp can be generated dynamically with strtotime () .
// 24. Dezember 1989 echo date_i18n( get_option( 'date_format' ), strtotime( '1989-12-24' ) );
Output the current time in the WordPress theme
The current time can easily be found using the same function. All you have to do is adjust the transferred format:
echo date_i18n( 'H:i' );
Alternatively, we can also use the time format set in the WordPress options:
echo date_i18n( get_option( 'time_format' ) );
Date and time can of course be combined:
echo date_i18n( 'dmY H:i' ); echo date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) );
Insert date with WordPress plugin as shortcode
Another option for inserting today's date is to use the Shortcode for Current Date plugin, which can be downloaded from WordPress.org. This means that the date can also be displayed without code and theme modifications.
Insert current Date, Month or Year anywhere in your WordPress site with a simple shortcode.
After installing the plugin, the current date can be inserted into any posts, pages or widgets using the shortcode [current_date] . A date format can also be passed as an argument here:
[current_date format='F d, Y']
The only drawback: The plugin also uses the unlocated function current_time () so far , so the month names always appear in English. I pointed out date_i18n () to the developer, maybe there will be an update for it.