PHP Date() Function
Posted by tutor | Posted in PHP Tutorial | Posted on 09-08-2010-05-2008
0
The built-in PHP date() function is used to format a timestamp to a more readable time and/or date, where A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
Syntax
- date(format,timestamp)
format- This is a required parameter which specifies the format of the timestamp.
timestamp- This is an optional parameter which specifies a timestamp. If the timestamp parameter is not provided the default is used which is the current date and time
PHP Date() – Format the Date
The format which is passed as parameter in the date() function specifies how to format the date/time, ie in dd-mm-yyyy format or any other.
It uses some characters to specify the format which are as:
- d – Represents the day of the month (01 to 31)
- m – Represents a month (01 to 12)
- Y – Represents a year (in four digits)
Other characters, like”/”, “.”, or “-” can also be inserted to specify the date separator
<?php
echo date(“Y/m/d”) . “<br />”;
echo date(“Y.m.d”) . “<br />”;
echo date(“Y-m-d”)
?>
2009/12/15
2009.12.15
2009-12-15
PHP Date() – Adding a Timestamp
Timestamp parameter specifies time. It is an optional parameter. In case the timestamp is not specified, the current date and time will be used.
The mktime() function returns the Unix format of 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.
Syntax for mktime()
- 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);
?>
Tomorrow is 2009/12/15
