How to get current date and time in WordPress?

16,133

You should use current_time function of WordPress instead of PHP date function for getting local date and time in WordPress. It will return correct value for you based on selected timezone in WordPress general options.

For me current_time('G') returns exactly 15 while PHP date('G') returns 10 which is not correct.

Hope it helps somebody since I didn't find related thread in the stackoverflow.

Share:
16,133
Hossein
Author by

Hossein

I'm Hossein Rafiei, An experienced PHP, Joomla, WordPress and Laravel Developer. I developed many extensions for Joomla and WordPress in the past 10 years. Although my focus is on backend development but I know frontend development well. Nice team player Talented Fast Learner Experienced Scrum Manager Worked with source control systems such as Git and SVN

Updated on July 06, 2022

Comments

  • Hossein
    Hossein almost 2 years

    As you may know it's highly recommended by WordPress to theme/plugin authors to don't change the WordPress timezone by PHP functions like date_default_timezone_set.

    I addition to that you may receive wrong date and time values when you use php date function because of not setting default timezone by PHP.

    I mean you may receive 10 for date('G') while the exact hour is 15 in the selected timezone in WordPress->Settings->General->Timezone option.

    So what should you do if you need the correct date and time values in selected timezone for WordPress website?