How do I get the current date and time in PHP? By using PHP's date function, which will return the server's current date and time.
echo date('d/m/Y H:i:s');
The date function will output:
03/05/2024 11:59:57
The parameters used in the above date example are as follows. Any other characters that don't match PHP's set date parameters are printed as is. In the above example that's "/" and ":".
'Y' - Four digit year
'm' - Two-digit month
'd' - Two-digit day
'H' - Hour 24-hour clock
'i' - Minute
's' - Second
If you already have a date that you need to convert you can use PHP's strtotime
.
echo date('H:i:s') . "\n";
# Outputs
12:05:35
echo date('H:i:s', strtotime('+5 hours'));
# Outputs
17:05:35
Get the current date and time in PHP using the time function
We can use the time() function in PHP to get the Unix timestamp of the current date and time.
echo time();
Will output the current Unix timestamp of the server.
1714768590
We've built a free tool that takes a Unix timestamp and converts it to a human-readable date. If you are having trouble with dates in PHP you can also check the last triggered error with PHP's built-in get last date error, which will display an array of warnings and errors whilst parsing a date time string.
var_dump(date_get_last_errors());