WP-Mix

A fresh mix of code snippets and tutorials

WordPress: Display Site Creation Date

I’m surprised that WordPress doesn’t provide a function for getting the site creation date. Knowing when the site was set up can be useful for all sorts of things. Copyright dates. Bragging rights. Whatever. If you want to display the date your site was first installed, here is one way to do it.

Get site creation year

To get the information we need, we look at the date on which the default user (with ID = 1) was created in the database:

<?php $date = mysql2date('Y', get_user_option('user_registered', 1)); ?>

So this snippet grabs the user registration date via get_user_option(), and then passes it through mysql2date(). The result will be the year the default user was created, which technically should be the same date as when WordPress was first installed. Yes it’s a bit hacky, but until a better way is found, this works just fine.

Get site creation Unix timestamp

Another code snippet for getting the creation date in Unix timestamp format:

<?php $date = mysql2date('U', '2018-12-29 9:54:18'); // 1546120458 ?>

The trick here is to use U as the first parameter of the mysql2date() function. The output will look like “1546120458”.

Get site creation day, month, and year

Here we get the creation date in day, month, year format:

<?php $date = mysql2date('l, F j, Y', '2018-12-29 9:54:18'); // Saturday, December 29, 2018 ?>

Notice the first argument, l, F j, Y passed to mysql2date(). That’s what determines how the output date information is formatted, in this case as “Saturday, December 29, 2018”. You can customize easily to get whatever format you need.

Fallback method: Display date of first published post

If for some reason, your WordPress site does not have the usual first-registered admin user (as used in the previous technique), here is a suitable fallback method. Instead of looking at the date of the first admin user, we get the date of the first published post. The code looks like this:

function shapeSpace_get_first_post_date() {
	
	$all_posts = get_posts(array('numberposts' => -1, 'post_status' => 'publish', 'order' => 'ASC'));
	
	$first_post = $all_posts[0];
	
	return date('Y-m-d', strtotime($first_post->post_date));
	
}

Once that code is in place, you can call it like so:

<?php echo shapeSpace_get_first_post_date(); ?>

Calling that from within your plugin or theme template will display the first post date in Y-m-d (Year month date) format. Note that this is a very basic example to give you an idea of how it works. I encourage you to expand the function by adding isset(), customize the date format, and modify things as needed. Make it yours.

Bonus: Get date of latest post

Here is a somewhat related technique that may be useful in this context:

<?php $date = get_post_time('U', true); ?>

This code snippet uses get_post_time() to get the date of the latest post. As with previous examples, the first parameter can be customized to output whatever date format is required.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress