Opened 9 months ago
Last modified 4 months ago
#21653 new enhancement
Introduce current_date()
| Reported by: |
|
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)
Change History (7)
johnbillion — 9 months ago
comment:1
johnbillion — 9 months ago
- Keywords has-patch added
comment:2
DrewAPicture — 9 months 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:
↓ 4
danielbachhuber — 4 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:
- Less code.
- Doesn't create two functions with slightly different use.
Patch is attached.
comment:4
in reply to:
↑ 3
johnbillion — 4 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.
comment:5
johnbillion — 4 months 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().

Patch which introduces current_date() and current_date_i18n().