How to format date and time in php function in wordpress?

Member

by darion , in category: PHP General , 9 months ago

How to format date and time in php function in wordpress?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 9 months ago

@darion 

In WordPress, you can format date and time using the date_i18n function which is a wrapper function for the PHP date function. Here's an example of how you can format date and time in WordPress using the date_i18n function:

1
2
3
4
5
6
7
// Current date and time
$current_datetime = current_time( 'timestamp' );

// Format date and time using date_i18n function
$formatted_datetime = date_i18n( 'F j, Y g:i a', $current_datetime );

echo $formatted_datetime;


In the example above, the date_i18n function is used to format the current date and time. The first parameter in the function specifies the format in which you want to display the date and time. In this case, 'F j, Y g:i a' is the format for displaying the date in the format "Month Day, Year Hour:Minute AM/PM".


You can customize the date and time format by referring to the PHP date function documentation (https://www.php.net/manual/en/function.date.php) for a list of formatting options that you can use with the date function.