Title: Replacement for current_time( &#8216;timestamp&#8217; )
Last modified: December 15, 2019

---

# Replacement for current_time( ‘timestamp’ )

 *  Resolved [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/)
 * Hi,
 * While reading the [thread](https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/)
   about date/time improvements I notice it’s discouraged to use the `current_time('
   timestamp' )`.
 * This function returns the **local** timestamp. I need that. But I cannot find
   a replacement for this, that returns a timestamp with local timezone included.
 * Have tried the new `current_datetime()` function but that one does not return
   a timestamp. So by using `strtotime()` I can generate a timestamp:
 *     ```
       $obj = current_datetime();
       $array = get_object_vars($obj);
       $my_current_time = strtotime($array['date']);
       ```
   
 * But timezone can be different when some plugin/theme sets another one by using
   function `date_default_timezone_set()`. So this I cannot fully trust.
 * Who can help me with this?
 * Guido

Viewing 15 replies - 1 through 15 (of 28 total)

1 [2](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/page/2/?output_format=md)

 *  [autotutorial](https://wordpress.org/support/users/autotutorial/)
 * (@autotutorial)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240179)
 * From WordPress 5.3 changes have been made to the datetime component, updated 
   code for the DateTime object.
    I currently proposed a change. [https://core.trac.wordpress.org/ticket/48976](https://core.trac.wordpress.org/ticket/48976)`
   date_default_timezone_set()` it has no effect on the DateTime object if a valid
   DateTimeZone is specified , it is for use of date or strtotime function, instead
   of working with date / strtotime functions, create your code with DateTime and
   DateTimeZone, if you use the syntax setTimeZone for DateTimeZone it is available
   from php 5.2.1 otherwise DateTime exists from php 5.1 but is enabled by php 5.2
   by default.
 *     ```
       $type = 'U';
       $gmt = 0;
       current_time($type,$type);
       ```
   
 * It should add the offset set in the general WordPress settings but does so only
   with my change.
    `$gmt = 1;` for UTC.
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240363)
 * Thanks for your response.
    I don’t understand why there’s no default (and still
   recomended) method / function that simply returns the local time of a site.
 * Guido
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240407)
 * The PHP `mktime()` function, called with no parameters, will do exactly what 
   you want.
 * [https://www.php.net/manual/en/function.mktime.php](https://www.php.net/manual/en/function.mktime.php)
 *  [autotutorial](https://wordpress.org/support/users/autotutorial/)
 * (@autotutorial)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240415)
 *     ```
       $array = current_datetime();
       $localtime = $array->getTimestamp()+$array->getOffset();
       var_dump( time(), $localtime );
       ```
   
 * I explained to you you will need to use the DateTime object instead of the old
   php functions.
    [@diondesigns](https://wordpress.org/support/users/diondesigns/)
 * > **Note:**
   > As of PHP 5.1, when called with no arguments, mktime() throws an E_STRICT notice:
   > use the time() function instead.
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240471)
 * Hi,
 * Aha, I understand now. Yes, that one does the job. I don’t have much knowledge
   about this, am still learning.
 * But thanks, I will use this.
 * Guido
 *  [autotutorial](https://wordpress.org/support/users/autotutorial/)
 * (@autotutorial)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240495)
 *     ```
       $array = current_datetime();
       //$localtime = $array->getTimestamp()+$array->getOffset();
   
       // Convert english date
       $datetime2 = $array->format('Y-m-d H:i:s');
       echo $datetime2."\n";
       $datetime3 = new DateTime( $datetime2 );
       $datetime3->setTimezone( new DateTimeZone('America/Atka') );
       echo $datetime3->format('Y-m-d H:i:s')."\n";
       ```
   
 * [Reference](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240179)
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240498)
 * Last question:
 * I echo the `$localtime` and a timestamp is being displayed.
 * Can you explain me how this part works:
 *     ```
       $localtime = $array->getTimestamp()+$array->getOffset();
       ```
   
 * Because the array itself returns something like this:
 *     ```
       DateTimeImmutable Object ( [date] => 2019-12-15 17:49:33.604710 [timezone_type] => 3 [timezone] => Europe/Amsterdam )
       ```
   
 * Guido
 *  [autotutorial](https://wordpress.org/support/users/autotutorial/)
 * (@autotutorial)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240519)
 * The names are self-explanatory, if you insert the **DateTime** object into a 
   variable you can access it through variable-name->method/property.
    getTimestamp
   retrieves the $array timestamp while getOffset retrieves the $array offset. [https://www.php.net/manual/en/datetime.construct.php](https://www.php.net/manual/en/datetime.construct.php)
   [https://www.php.net/manual/en/datetime.gettimestamp.php](https://www.php.net/manual/en/datetime.gettimestamp.php)
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12240750)
 * > getTimestamp retrieves the $array timestamp while getOffset retrieves the $
   > array offset.
 * Please take a look at the object / array itself:
 *     ```
       DateTimeImmutable Object ( [date] => 2019-12-15 17:49:33.604710 [timezone_type] => 3 [timezone] => Europe/Amsterdam )
       ```
   
 * Is `getTimestamp` > `[date]`?
    And is `getOffset` > `[timezone]`?
 * Guido
 *  [autotutorial](https://wordpress.org/support/users/autotutorial/)
 * (@autotutorial)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12242883)
 * I [@guido07111975](https://wordpress.org/support/users/guido07111975/) is that
   DateTime object.
    But there is no need to convert to timestamps if you need the
   time used in your general settings. can you show me the case of using the timestamp?
   if you use the old php function you have to calculate the offset of your php 
   while if you use the DateTime object it will be converted automatically if you
   insert it in DateTimeZone.
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12242917)
 * Hi,
 * I think we don’t fully understand each other.
 * You code works great for local time, so I will replace my `current_time('timestamp')`
   with your code:
 *     ```
       $array = current_datetime();
       $localtime = $array->getTimestamp()+$array->getOffset();
       ```
   
 * I just wanted to know **how** `getTimestamp` and `getOffset` are being extracted
   from the `$array`. That’s why I posted my previous reply.
 * Guido
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12242973)
 * Update: both are part of [DateTimeImmutable](https://www.php.net/manual/en/class.datetimeimmutable.php),
   am I right?
 * Guido
 *  [autotutorial](https://wordpress.org/support/users/autotutorial/)
 * (@autotutorial)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12243007)
 * Guido yes, DateImmutable returns a non-modifiable object useful in situations
   where it could be modified and this is the strength of current_datetime.
    It 
   is difficult to teach how to code if you have not studied programming, you must
   read a good php book.
 *     ```
       $array = current_datetime();
       $localtime = $array->getTimestamp()+$array->getOffset();
       ```
   
 * $array is the variable with the value of the object, to read the methods or properties
   we use `->`, if the variable é $array each property or method refers to the object
   inside $array
 *  Thread Starter [Guido](https://wordpress.org/support/users/guido07111975/)
 * (@guido07111975)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12243100)
 * Many thanks, I now fully understand how function `current_datetime()` works.
 * This thread has status “resolved” now.
 * Guido
 *  [autotutorial](https://wordpress.org/support/users/autotutorial/)
 * (@autotutorial)
 * [6 years, 5 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/#post-12243160)
 * I’m glad you solved, I update the topic that from WordPress 5.3 current_time 
   always returns the correct time, before WordPress 5.3 **maybe** the correct time.

Viewing 15 replies - 1 through 15 (of 28 total)

1 [2](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/page/2/?output_format=md)

The topic ‘Replacement for current_time( ‘timestamp’ )’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 28 replies
 * 3 participants
 * Last reply from: [Guido](https://wordpress.org/support/users/guido07111975/)
 * Last activity: [6 years, 4 months ago](https://wordpress.org/support/topic/replacement-for-current_time-timestamp/page/2/#post-12269296)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
