current_time('timestamp') seems to be different from the real current time

Does current_time('timestamp') have an issue?

I am trying to get the current_time('timestamp'), and instead of giving me my current time:

Jun 26 2013 14:30

It gives me:

Jun 26 2013 21:30

I tried to check the seconds, and it gives me:

1372282238

Which is correct for the time given, but not for the real current time. What's happening?

Topic timestamp functions Wordpress

Category Web


If you're dealing with a legacy code base where you are comparing different "timestamp" style numbers and you don't have time to totally refactor everything to use nice modern DateTime objects and comparisons, you can just do something like this in order to just get things to pass phpcs:

// legacy code
if ( strtotime( $some_date ) >= current_time( 'timestamp' ) ):
// quickfix
if ( strtotime( $some_date ) >= strtotime( current_time( 'mysql' ) ) :

This, I think, addresses the concern in the PHPCS warning message - that current_time( 'timestamp' ) is misleading and discouraged because the word "timestamp" should always mean a GMT timestamp, and this outputs the timestamp with the WP timezone offset.


A call current_time( 'timestamp' ) does not strictly speaking give a timestamp, as it depends on the timezone in the WordPress settings. Actual timestamps are always UTC:

Unix timestamps are always in UTC and do not have any other timezone attached.

(from Codex https://codex.wordpress.org/Function_Reference/current_time)

The use of current_time( 'timestamp' ) is currently discouraged: https://core.trac.wordpress.org/ticket/40657


It's a WP non-code programming thinking error.

Under General Settings > Timezone

It should be set to your own timezone.


The output of current_time('timestamp') should be

time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );

according to WP 3.5.2, so you should check your get_option( 'gmt_offset' ) settings.

Also current_time('timestamp', 1 ) should give you time().

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.