Opened 9 months ago

Last modified 4 months ago

#21653 new enhancement

Introduce current_date()

Reported by: johnbillion Owned by:
Priority: low Milestone: Awaiting Review
Component: Date/Time Version:
Severity: normal Keywords: has-patch
Cc: xoodrew@…, danielbachhuber

Description

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 9 months ago.
21653.2.patch (1.2 KB) - added by danielbachhuber 4 months ago.
Offer PHP date format support to current_time()

Download all attachments as: .zip

Change History (7)

  • Keywords has-patch added

Patch which introduces current_date() and current_date_i18n().

  • 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: ↓ 4   danielbachhuber4 months 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()?

Usage:

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 ) ) );
			break;
		case 'timestamp':
			return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
			break;
		default:
			return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
			break;
	}
}

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.

Offer PHP date format support to current_time()

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

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

Note: See TracTickets for help on using tickets.