Display the Current Date
If you want to display the current date, you can use the following code:
<?php $current_timestamp = date('m/d/Y h:i:s a', time()); echo $current_timestamp; ?>
The following will output something like:
07/30/2022 03:47:56 pm
By calling the time function with time(), you get the number of seconds since Unix Epoch (January 1 1970 00:00:00 GMT).
The date function formats the time, converting the seconds into the month, day, year, hour, minute, and second. You can control the display by changing the characters that represent what you want it to show.
Format Character | Description | Example Returned Values |
---|---|---|
m | Numeric representation of a month, with leading zeros | 01 through 12 |
d | Day of the month, 2 digits with leading zeros | 01 to 31 |
Y | A full numeric representation of a year, at least 4 digits, with - for years BCE. | Examples: -0055 , 0787 , 1999 , 2003 , 10191 |
h | 12-hour format of an hour with leading zeros | 01 through 12 |
i | Minutes with leading zeros | 00 to 59 |
s | Seconds with leading zeros | 00 through 59 |
a | Lowercase Ante meridiem and Post meridiem | am or pm |
For a full list, you can refer to the PHP documentation, which can be found here.