0

In my plugin, I am trying to get the current date/time of the blog into the code but it seems to be returning a odd date/time. If i put a new post on the blog it does show the correct date/time so I am not sure what the issue is. Am I handling the code incorrectly? (Post Time: Published on: Feb 13, 2012 @ 22:45)

PHP Code

<?
$t_time = get_the_time( __( 'Y/m/d g:i:s A' ) );
echo "<h1>BLOG TIME</h1>";
echo $t_time . "<br>";
echo "<br>";
echo "<h1>SERVER TIME</h1>";
echo date("Y/m/d g:i:s A");
?>

OUTPUT BLOG TIME 2011/05/20 10:28:16 PM

SERVER TIME 2012/02/14 5:41:35 AM

3 Answers 3

10

There is a function in WordPress called current_time(); which you pass either 'timestamp' or 'mysql' to and it returns a time.

For more information: https://developer.wordpress.org/reference/functions/current_time/

2
  • +1. I would add that WordPress forces the server timezone to be UTC, which is why you should use WordPress' functions (as they take this into account) rather than native PHP ones. Commented Feb 14, 2012 at 9:37
  • 1
    As time has moved on, this answer is no longer useful nor correct. current_time() returns unix time() plus the timezone offset, in effect, what is called "wordpress timestamp". Usage with 'timestamp' is now deprecated.
    – Brian C
    Commented Jun 13, 2021 at 5:13
3

Since WordPress 5.3 current_time( 'timestamp' ) is No longer recommended. If you need to get a time that is adjusted for the site's timezone setting, the following functions are recommended to use:

 1. current_datetime()
 2. time()
 3. get_post_datetime()
 4. get_post_timestamp()

Why this change?

Date/Time component relied on WordPress timestamp (before WordPress 5.3), this was causing many bugs and lack of ability to go with upstream PHP.

Detail information here: https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/

1

Code

The current (2020) correct code to display a full date and time in localized form is via the new WordPress core function wp_date():

echo wp_date( get_option( 'date_format' ), get_post_timestamp() )
   . wp_date( get_option( 'time_format' ), get_post_timestamp() );

This relatively short code snippet:

  • obeys the requested formatting options for date and time from WordPress Settings
  • does the appropriate translations
  • applies the correct timezone from Settings
  • is considered best practice in 2021 and forward.

Rationale

With the release of wp_date() in WordPress 5.3 (2019), WordPress has moved a long way towards fixing the confusing and broken way that time worked within WordPress, specifically, the problem where WordPress timestamps were not the same as Unix timestamps (aka the value of time()) but had the timezone offset added. The confusion arose because in the Unix world, the output of time() (aka "Unix timestamp") was historically always in GMT form, and was compounded by the use of the term "unix timestamp" to describe these "WordPress timestamps". The new function wp_date() does the localization you'd normally need and works correctly with supplied GMT timestamp values.

Note that using PHP's date_default_timezone_set() to set a site timezone (other than locally) will still break WordPress, but now there are nice alternatives that work as one would expect. It's worth noting that the use of date_default_timezone_set() is now detected in Site Health as 'critical' since it will break time handling within WordPress and some plugins; if you do use it in old code you should remove it as soon as you can and replace your use of date() with wp_date().

Documentation

A more complete introduction to the new date and time functions, written by the team that developed the new code, is here: https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/

Note that some of the new functions return PHP DateTimeImmutable objects which makes life considerably easier. Additionally all of the functions listed in the linked introduction are now core and thus have core PHP manual pages in the usual places.

Also, they do include notes on what to use, and what not to use (though at the moment, they lack code examples).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.