Sometimnes, we want to get the current date and time in PHP.
In this artricle, we’ll look at how to get the current date and time in PHP.
How to get the current date and time in PHP?
To get the current date and time in PHP, we can use the date
function.
For instance, we write
$date = date('Y-m-d H:i:s');
to call date
with a format string to return a date string in the given format.
Y
is the year. m
is the month. d
is the day.
H
is the hour. i
is the minute. s
is the second.
This will return current date time at the server’s local time zone.
We can change ther time zone with date_default_timezone_set
.
For instance, we write
date_default_timezone_set('America/Los_Angeles');
$date = date('Y-m-d H:i:s');
to call date_default_timezone_set
to set the time zone.
Then we call date
to get the current date and time at the current time zone.
Conclusion
To get the current date and time in PHP, we can use the date
function.