Snippets > PHP Date & Time
How to utilise date and time in PHP.
// 2021-07-01 15:44:00
$mysql_datetime = date("Y-m-d H:i:s");
// Thursday 1st July 2021
$human_datetime = date("l jS F Y");
// Convert from 2021-03-01 to 01/03/2021
$change_format = date("d/m/Y", strtotime('2021-03-01'));
// OOP from 2024-02-22 to 22/02/2024
$datetime = DateTime::createFromFormat('Y-m-d', '2024-02-22');
$date_formatted = $datetime->format('d/m/Y');
// OOP current date in Y-m-d H:i:s format
$datetime= new DateTime();
$mysql_datetime = $datetime->format('Y-m-d H:i:s');
When using strtotime():
Date values separated by slash are assumed to be in American order: m/d/y
Date values separated by dash are assumed to be in European order: d-m-y
The following characters are recognized in the format parameter string:
Character | Description |
---|---|
Y | A full numeric representation of a year, 4 digits |
m | Numeric representation of a month, with leading zeros |
d | Day of the month, 2 digits with leading zeros |
H | 24-hour format of an hour with leading zeros |
m | Numeric representation of a month, with leading zeros |
i | Minutes with leading zeros |
l | (lowercase 'L') A full textual representation of the day of the week |
j | Day of the month without leading zeros |
S | English ordinal suffix for the day of the month, 2 characters |
F | A full textual representation of a month, such as January or March |