A fresh mix of code snippets and tutorials

Update WordPress Post With Current Time

Here is a simple technique for updating WordPress posts with the current date and time. Useful for timezone/server transfers.

Update! WordPress 5.3 changes date/time functionality. Check out this post for more details and all the latest techniques.</update>

If you’re working with a WordPress-powered site and notice that all date/time information is messed up, you can add the following to the WordPress Loop:

$current_time = current_time('mysql');
	array (
		'ID'            => get_the_ID(),
		'post_date'     => $current_time,
		'post_date_gmt' => get_gmt_from_date($current_time)

The array that we are passing represents the elements that make up a post. There is a one-to-one relationship between these elements and the names of columns in the wp_posts table in the database. So you can get as specific (or not) as desired.

The key for this technique is to make sure that the post ID, date, and GMT date all are passed with the correct values. And of course we are getting the current time using PHP’s current_time() function.

Note: as explained in the WP Codex, it is important to use this function only in development environments. Learn more details by visiting the reference link below.


Related Posts

Learn more

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