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.
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.