wordpress get local date

To get the local date in WordPress, you can follow these steps:

  1. Use the date_i18n() function: WordPress provides a built-in function called date_i18n() that returns the local date and time based on the timezone set in the WordPress settings.

  2. Specify the date format: The date_i18n() function accepts two parameters. The first parameter is the date format, which determines how the date will be displayed. You can use various format characters to customize the output according to your requirements. For example, 'F j, Y' will display the date as 'December 12, 2023'.

  3. Set the timezone: By default, the date_i18n() function uses the timezone set in the WordPress settings. If you want to display the date in a different timezone, you can use the date_default_timezone_set() function to set a different timezone before calling the date_i18n() function. For example, date_default_timezone_set('America/New_York') will set the timezone to Eastern Standard Time.

  4. Display the local date: Finally, you can echo or print the result of the date_i18n() function to display the local date on your WordPress website. For example, echo date_i18n('F j, Y') will output the current local date in the specified format.

These steps will allow you to retrieve and display the local date in WordPress, taking into account the timezone settings and any custom date formats you specify.