How to get the current date and time in PHP?

Member

by rollin , in category: PHP General , 6 months ago

How to get the current date and time in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 4 months ago

@rollin 

The current date and time in PHP can be obtained using the built-in function date(). The function takes a format parameter that defines how the date should be displayed.


Here's an example of how to get the current date and time in the default format:

1
2
$currentDate = date('Y-m-d H:i:s');
echo $currentDate;


This will output something like: 2021-06-15 12:34:56


You can customize the format of the output by changing the format parameter. For example, to display the date in a more human-readable format, you can use:

1
2
$currentDate = date('F j, Y, g:i a');
echo $currentDate;


This will output something like: June 15, 2021, 12:34 pm