0

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?

1
  • It's probably an issue with time zones.
    – Flimm
    Commented Dec 2 at 14:24

4 Answers 4

3

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

0
2

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

Under General Settings > Timezone

It should be set to your own timezone.

1

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

1
  • Sadly, timestamps are not always in UTC. There are some implementations of timestamps that count the number of seconds since 1970-01-01T00:00:00 in an unspecified time zone, rather than UTC. So it's worth checking.
    – Flimm
    Commented Dec 2 at 14:08
0

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.

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.