1

How to add 2 hours to the wordpress time which has already been formed using current_time("mysql", false)?

  • Second param on that function is an integer, not a boolean. Are you getting GMT result or is that is simply being ignored? – jdm2112 May 30 '18 at 16:14
  • From the Codex: If the optional second parameter is 1, the value returned represents the current GMT time. If 0 or no second parameter are set, the value returned represents the local time for the timezone declared in the blog's Timezone setting on the General Settings page. – jdm2112 May 30 '18 at 16:14
  • Yeah, but I want to add two hours to the local time for the timezone declared... – Vladimir Despotovic May 30 '18 at 16:22
  • ahaaaa, General Settings, great!!! – Vladimir Despotovic May 30 '18 at 16:23
  • please make this an answer, so that I can accept it? Thanks. – Vladimir Despotovic May 30 '18 at 16:23
1

The WordPress current_time() returns the current time in the format specified in the first parameter. In your example code, this is the mysql value. The second parameter represents a timezone option. The default returns the local time for the timezone specified on the site's Settings > General page, or GMT depending on the value supplied when calling the function.

https://codex.wordpress.org/Function_Reference/current_time

If time mathematics are still required, using the PHP strtotime() function is useful for this:

$now = current_time( 'mysql' ); 
echo 'It is currently: ' . $now . '<br>';
$later = date( 'Y-m-d H:i:s', strtotime( $now ) + 7200 ); //7200 seconds = 2 hours
echo 'In 2 hours it will be: ' . $later;
  • So you think that $later now - per your explanation - has the correct format for insertion to Wordpress DB? – Vladimir Despotovic May 30 '18 at 19:50
  • 1
    That depends. The date is just a string until a portion of code treats it as a date, right? You can store the date in any format as long as you handle it properly when querying the DB. codex.wordpress.org/Formatting_Date_and_Time – jdm2112 May 30 '18 at 20:06
  • Yeah, it doesn't say anything about insertion to database, so I wouldn't know how to do it. It's a matter for another question though. – Vladimir Despotovic May 30 '18 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.