Codex

Function Reference/current time

Contents

Description

The function current_time("mysql", $gmt) returns the time formatted as 'Y-m-d H:i:s'. If $gmt=1, the time returned is GMT; if $gmt=0, the time returned is the browser client local time (as determined by WordPress option gmt_offset, set as Timezone on General Settings page).

WARNING: current_time('timestamp',1) returns (as a timestamp) the server time, not (as expected) GMT! Because this is exactly what PHP's time() returns, current_time('timestamp',1) is superfluous and unnecessary -- use time().

WARNING: current_time('timestamp',0) returns the timestamp GMT + gmt_offset(server) + gmt_offset(browser) -- a meaningless construct.

It is difficult to imagine any use for the 'timestamp' parameter value.

Usage

 current_time($type, $gmt = 0); 

Parameters

$type
(string) (required) The time format to return. Possible values:
  • mysql
  • timestamp
Default: None
$gmt
(integer) (optional) The time zone (GMT, local) of the returned time: Possible values:
  • 1
  • 0
Default: 0

Example

This example gets the current time and assigns the parameters to variables.

<?php 
$blogtime = current_time('mysql'); 
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = split( '([^0-9])', $blogtime );
?>
Example of format of current_time('mysql'):
   2005-08-05 10:41:13


Examine the results

Put this on a WordPress template and run it from a server in a different time zone (not your W/LAMP localhost):

<?php echo "current_time('mysql') returns local server time: " . current_time('mysql') . '<br />'; ?>
<?php echo "current_time('mysql',1) returns GMT: " . current_time('mysql',1) . '<br />'; ?>
<?php echo "current_time('timestamp',1) returns timestamp of server time: " . date('Y-m-d H:i:s',current_time('timestamp',1)); ?>
<?php echo "current_time('timestamp',0) doesn't mean anything: " . date('Y-m-d H:i:s',current_time('timestamp',0)); ?>


Correction

If the user needs a correctly functioning current_time('timestamp', $gmt=0), the following will work, satisfying this description:

Description

The function current_time_fixed("mysql", $gmt) returns the time formatted as 'Y-m-d H:i:s'. The function current_time_fixed("timestamp", $gmt) returns the time as a Unix timestamp. If $gmt=1, the time returned is GMT; if $gmt=0, the time returned is the browser client local time (as determined by WordPress option gmt_offset, set as Timezone on General Settings page).

function current_time_fixed( $type, $gmt = 0 ) {
	$t =  ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
	switch ( $type ) {
		case 'mysql':
			return $t;
			break;
		case 'timestamp':
			return strtotime($t);
			break;
	}
}
This article is marked as in need of editing. You can help Codex by editing it.