Make WordPress Core

Opened 12 years ago

Closed 10 years ago

Last modified 8 years ago

#21653 closed enhancement (fixed)

Introduce current_date()

Reported by: johnbillion's profile johnbillion Owned by: nacin's profile nacin
Milestone: 3.9 Priority: low
Severity: normal Version:
Component: Date/Time Keywords: has-patch commit
Focuses: Cc:


WordPress comes with a function called current_time() which returns the current time according to the timezone chosen on the General Settings screen. It's used when populating the post date, among other things.

It would be nice to have a date equivalent of this function for outputting the current date according to the timezone setting. Currently if you want to output the current date you need to do this:

echo date( 'd/m/Y', current_time( 'timestamp' ) );

I'd like to be able to use this instead:

echo current_date( 'd/m/Y' );

Note that just using date() doesn't give the desired effect as WordPress sets the default timezone (as used by date()) to UTC, not to the timezone chosen on the settings screen.

Attachments (2)

21653.patch (1.1 KB) - added by johnbillion 12 years ago.
21653.2.patch (1.2 KB) - added by danielbachhuber 11 years ago.
Offer PHP date format support to current_time()

Download all attachments as: .zip

Change History (14)

12 years ago

#1 @johnbillion
12 years ago

  • Keywords has-patch added

Patch which introduces current_date() and current_date_i18n().

#2 @DrewAPicture
12 years ago

  • Cc xoodrew@… added

+1 for 21653.patch. I've been using a very similar wrapper for current_time() because of project recently where there was a ton of date-sensitive functionality.

#3 follow-up: @danielbachhuber
11 years ago

  • Cc danielbachhuber added
  • Component changed from General to Date/Time

Rather than introduce a new function which accepts slightly different params, why not add PHP date format to current_time()?


current_time( 'Y-m-d' );

How it'd work:

function current_time( $type, $gmt = 0 ) {
	switch ( $type ) {
		case 'mysql':
			return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
		case 'timestamp':
			return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
			return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );

Although the naming deviates from PHP, I believe this approach is superior for a couple of reasons:

  1. Less code.
  2. Doesn't create two functions with slightly different use.

Patch is attached.

11 years ago

Offer PHP date format support to current_time()

#4 in reply to: ↑ 3 @johnbillion
11 years ago

Replying to danielbachhuber:

Rather than introduce a new function which accepts slightly different params, why not add PHP date format to current_time()?

+1. 21653.2.patch looks good.

#5 @johnbillion
11 years ago

Oh hang on, my first patch also introduces current_date_i18n() which outputs localised date formats. We'd need an equivalent current_time_i18n().

This ticket was mentioned in IRC in #wordpress-dev by danielbachhuber. View the logs.

10 years ago

#7 @nacin
10 years ago

  • Keywords commit added

I like danielbachhuber's approach.

Without current_date(), current_date_i18n() seems out of place. And, date_i18n() with only one argument already operates on the current, local timestamp (it has an optional gmt flag as well), so it's not needed.

I doubt current_time() has unit tests, but it'd be nice.

#8 @danielbachhuber
10 years ago

  • Milestone changed from Awaiting Review to 3.9

I doubt current_time() has unit tests, but it'd be nice.

How would you recommend mocking time() such that it's unit testable?

#9 @nacin
10 years ago

Yeah, this is actually not too easy to test at all. Even something like asserting 'Y' will need timezone offset calculations so it doesn't fail on December 31/January 1 every year.

#10 @nacin
10 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 27259:

Allow current_time() to accept a date format string, adding to 'timestamp' and 'mysql'.

props danielbachhuber.
fixes #21653.

#11 @Turn On Social
8 years ago

  • Summary changed from Introduce current_date() to Fix current_date()

'timestamp' functionality appears to be wrong.

current_time('timestamp') should always return the timestamp according to UTC. That's because a timestamp is defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970.

Therefore, producing this offset by x number of hours for local time is incorrect logic. If it's 1,452,598,896 seconds since the start of Unix time, this does not change depending on timezone - the number of seconds is the same (but may instead be displayed differently depending on timezone).

Last edited 8 years ago by Turn On Social (previous) (diff)

#12 @johnbillion
8 years ago

  • Summary changed from Fix current_date() to Introduce current_date()

@turn-on-social The current_time() function is explicitly designed to return a timestamp with the current site's timezone offset applied. Its return value is used as the $timestamp argument in functions such as date() because WordPress sets the server's timezone to UTC.

If you need the current UTC timestampe, use time().

If you think there's a real bug here please open a new ticket with these details, as this ticket was closed on a completed milestone. Thanks!

Note: See TracTickets for help on using tickets.