PHP Date and Time

The PHP date() function is used to format a date and/or a time.
The PHP date() function formats a timestamp to a more readable date and time.


A timestamp is a sequence of 
characters, denoting the date and/or time at which a certain event occurred.
The required format parameter of the date() function specifies how to format the date 
(or time).
Here are some characters that are commonly used for dates:
d - Represents the day of the month (01 to 31) 
m - Represents a month (01 to 12)
Y - Represents a year (in four digits)
l (lowercase 'L') - Represents the day of the week
Other characters, like"/", ".", or "-" can also be inserted between the 
characters to add additional formatting.
The example below formats today's date in three different ways:
Use the date() function to automatically update the copyright year on your 
website:
Here are some characters that are commonly used for times:
H - 24-hour format of an hour (00 to 23)
h - 12-hour format of an hour with leading zeros (01 to 12) 
i - Minutes with leading zeros (00 to 59)
s - Seconds with leading zeros (00 to 59)
a - Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:


Note that the PHP date() function will return the current date/time of the 
server!
If the time you got back from the code is not correct, it's probably because your server is in another country or set up 
for a different timezone.
So, if you need the time to be correct according to a specific 
location, you can set the timezone you want to use. 
The example below sets the timezone to "America/New_York", then outputs the current time in the specified format:
The optional timestamp parameter in the date() function specifies a timestamp. If 
omitted, the current date and time will be used (as in the 
examples above).
The PHP mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch 
(January 1 1970 00:00:00 GMT) and the time specified.
The example below creates a date and time with the 
date() function from a number of parameters in the 
mktime() function:
The PHP strtotime() function is used to convert a human readable 
date string into a 
Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
The example below creates a date and time from the strtotime() function:
PHP is quite clever about converting a string to a date, so you can put in 
various values:
However, strtotime() is not perfect, so remember to check the strings you put in there.
The example below outputs the dates for the next six Saturdays:
The example below outputs the number of days until 4th of July:
For a complete reference of all date functions, go to our complete
PHP Date Reference.
The reference contains a brief description, and examples of use, for each function!
Use the correct date function to output the weekday name of today (monday, tuesday etc.).
echo ;

We just launchedW3Schools videos
Get certifiedby completinga course today!
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
help@w3schools.com
Your message has been sent to W3Schools.