Get date format admin setting in WordPress

If you're writing plugins for WordPress for other people to use, it's helpful to display any dates in the format they have defined in the admin settings section. This snippet shows you how to retrieve and use that setting.

The local date format for WordPress is stored in the options table as date_format, thus it can be easily retrieved using the built in WordPress 'get_option' function like so:

$date_format = get_option( 'date_format' );

You can then use this in the PHP date function to format the current time, or a timestamp stored in a variable:

echo date( $date_format, time() );

echo date( $date_format, $timestamp );

You could of course also shorten this onto one line:

echo date( get_option( 'date_format', $timestamp );

Adding the time

If you also want to add the time to your date you can do this in a similar way by getting the option 'time_format' using the following function:

$time_format = get_option( 'time_format' );

So for a complete date and time using the WordPress admin settings and a custom date you could do the following:

echo date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp );

Notice how a space seperator has been placed between the two options to make it easier to read. 

Sign Up

NEXT: Use WordPress functions in a custom PHP file

How to use all the WordPress functions in a custom PHP file.

comments powered by Disqus

Popular Tags

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.