The PHP date() function is used to format a time or a date.
--------------------------------------------------------------------------------
[color=FA0309]The PHP Date() Function[/color]
The PHP date() function formats a timestamp to a more readable date and time. [color=FA0309]Syntax[/color]
date(format,timestamp) format : Required. Specifies the format of the timestamp
timestamp : Optional. Specifies a timestamp. Default is the current date and time (as a timestamp) -------------------------------------------------------------------------------- [color=FA0309]PHP Date - What is a Timestamp?[/color]
A timestamp is the number of seconds since January 1, 1970 at 00:00:00 GMT. This is also known as the Unix Timestamp. -------------------------------------------------------------------------------- [color=FA0309]PHP Date - Format the Date[/color]
The first parameter in the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some of the letters that can be used: - d - The day of the month (01-31)
- m - The current month, as a number (01-12)
- Y - The current year in four digits
An overview of all the letters that can be used in the format parameter . Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
The second parameter in the date() function specifies a timestamp. This parameter is optional. If you do not supply a timestamp, the current time will be used. In our next example we will use the mktime() function to create a timestamp for tomorrow. The mktime() function returns the Unix timestamp for a specified date. [color=FA0309]Syntax[/color]
The PHP date() function formats a timestamp to a more readable date and time. [color=FA0309]Syntax[/color]
date(format,timestamp) format : Required. Specifies the format of the timestamp
timestamp : Optional. Specifies a timestamp. Default is the current date and time (as a timestamp) -------------------------------------------------------------------------------- [color=FA0309]PHP Date - What is a Timestamp?[/color]
A timestamp is the number of seconds since January 1, 1970 at 00:00:00 GMT. This is also known as the Unix Timestamp. -------------------------------------------------------------------------------- [color=FA0309]PHP Date - Format the Date[/color]
The first parameter in the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some of the letters that can be used: - d - The day of the month (01-31)
- m - The current month, as a number (01-12)
- Y - The current year in four digits
An overview of all the letters that can be used in the format parameter . Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
<?php
echo date("Y/m/d");
echo "<br />";
echo date("Y.m.d");
echo "<br />";
echo date("Y-m-d");
?>
The output of the code above could be something like this:
echo date("Y/m/d");
echo "<br />";
echo date("Y.m.d");
echo "<br />";
echo date("Y-m-d");
?>
2007/07/11
2007.07.11
2007-07-11
--------------------------------------------------------------------------------
[color=FA0309]PHP Date - Adding a Timestamp[/color]2007.07.11
2007-07-11
The second parameter in the date() function specifies a timestamp. This parameter is optional. If you do not supply a timestamp, the current time will be used. In our next example we will use the mktime() function to create a timestamp for tomorrow. The mktime() function returns the Unix timestamp for a specified date. [color=FA0309]Syntax[/color]
mktime(hour,minute,second,month,day,year,is_dst)
To go one day in the future we simply add one to the day argument of mktime():
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));echo "Tomorrow is ".date("Y/m/d/", $tomorrow);
?>
The output of the code above could be something like this:
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));echo "Tomorrow is ".date("Y/m/d/", $tomorrow);
?>
Tomorrow is 2006/07/12