There are multiple ways which can be used to get PHP Current Date, I’m going to start with the simplest way.
date will return a
$Date = date('Y-m-d H:i:s');
echo $Date; // Will Output the current date in Y-m-d H:i:s format.
According to PHP Manual,
date() takes two parameters, the first one is
format the second one is
Breaking down the Format i used:
|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|
|i||Minutes with leading zeros|
|s||Minutes with leading zeros|
|–||dash will be rendered as it is, since it’s not a format code|
format is how you want the date to be outputted, for a list of available formats go to php.net.
that’s the simplest way to output a date. you can specify the
timestampif you don’t want the date to be the current date/time, using the function
time(), or from a valid timestamp string.
The Second way is using the DateTime() class.
$Date = (new DateTime("NOW"))->format('Y-m-d H:i:s');
echo $Date; // Will print the date in Y-m-d H:i:s format
DateTime can do much more than what
date can do, i will cover that in another post.
That’s it for now, a very simple post covering PHP Date.