Retrieves the current time based on specified type.
Description
- The ‘mysql’ type will return the time in the format for MySQL DATETIME field.
- The ‘timestamp’ or ‘U’ types will return the current timestamp or a sum of timestamp and timezone offset, depending on
$gmt
. - Other strings will be interpreted as PHP date formats (e.g. ‘Y-m-d’).
- The ‘timestamp’ or ‘U’ types will return the current timestamp or a sum of timestamp and timezone offset, depending on
If $gmt
is a truthy value then both types will use GMT time, otherwise the output is adjusted with the GMT offset for the site.
Parameters
$type
stringrequired- Type of time to retrieve. Accepts
'mysql'
,'timestamp'
,'U'
, or PHP date format string (e.g.'Y-m-d'
). $gmt
int|booloptional- Whether to use GMT timezone. Default false.
Source
* @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U',
* or PHP date format string (e.g. 'Y-m-d').
* @param int|bool $gmt Optional. Whether to use GMT timezone. Default false.
* @return int|string Integer if `$type` is 'timestamp' or 'U', string otherwise.
*/
function current_time( $type, $gmt = 0 ) {
// Don't use non-GMT timestamp, unless you know the difference and really need to.
if ( 'timestamp' === $type || 'U' === $type ) {
return $gmt ? time() : time() + (int) ( (float) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
}
if ( 'mysql' === $type ) {
$type = 'Y-m-d H:i:s';
}
The Date/Time component will be updated in WordPress 5.3, and there are some things that people should be aware of:
https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/
From the post:
Not recommended
Don’t retrieve time as WP timestamp:
Don’t localize time based on WP timestamp:
Don’t store WP timestamps persistently;
Don’t compare WP timestamps.
——————-
Recommended
Retrieve time as Unix timestamp or
DateTimeImmutable
object:Localize time based on Unix timestamp:
Store Unix timestamps or formats that are precise moment in time, such as
DATE_RFC3339
;Compare Unix timestamps,
DateTimeInterface
objects, or string–comparable dates in same time zone.Examine the results
The code snippet gives an Warning with “split” function because
the function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.
Alternatives to this function include:
preg_split()
On using preg_split(), we get the required output. Code snippet below:
$blogtime = current_time( 'mysql' );
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = preg_split( "([^0-9])", $blogtime );
echo $hour;
For reference:
http://php.net/manual/en/function.split.php
Since version 5.3, the code below is discouraged as it will not return a Unix (UTC) timestamp.
Here’s the alternative solution:
PHP date formats accepted for $type are defined at http://php.net/manual/en/function.date.php#refsect1-function.date-parameters
When working with time functions, you must use
current_time('timestamp')
NOTtime()
.current_time('timestamp')
return blog specific timestamp that is set under Settings->General.time()
return the time based ondate.timezone
setting from php.ini.Conclusion:
time() !== current_time('timestamp') // There is a big chance that they are not equal
Always use: current_time(‘timestamp’)
This example gets the current time and assigns the parameters to variables.
Example of format of
current_time( 'mysql' )
:2005-08-05 10:41:13