Make WordPress Core

Opened 3 years ago

Closed 20 months ago

#21653 closed enhancement (fixed)

Introduce current_date()

Reported by: johnbillion Owned by: 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 3 years ago.
21653.2.patch (1.2 KB) - added by danielbachhuber 3 years ago.
Offer PHP date format support to current_time()

Download all attachments as: .zip

Change History (12)

@johnbillion3 years ago

comment:1 @johnbillion3 years ago

  • Keywords has-patch added

Patch which introduces current_date() and current_date_i18n().

comment:2 @DrewAPicture3 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.

comment:3 follow-up: @danielbachhuber3 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.

@danielbachhuber3 years ago

Offer PHP date format support to current_time()

comment:4 in reply to: ↑ 3 @johnbillion3 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.

comment:5 @johnbillion3 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().

comment:6 @ircbot20 months ago

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

comment:7 @nacin20 months 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.

comment:8 @danielbachhuber20 months 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?

comment:9 @nacin20 months 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.

comment:10 @nacin20 months 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.

Note: See TracTickets for help on using tickets.