Date() function in php and advanced part? yes it is quite advanced when compared with other simple examples which were shown earlier.
It’s just a single function!! but still it has so many features it becomes complex.Let’s have a look at Date() function in php.
The PHP date() function is used to format a time and/or date.
Date() function returns a TIMESTAMP which is time+date together. It provides us an ability of Formatting the output in better readable format so that it is understandable.
Timestamp is very essential when it comes to creating and maintaining websites.. You might think why is it needed? You can log your visitors by the Timestamp and IP. You can use timestamp to store the things in database, and afterwards timestamp might be used to arrange the things in sequence.
A timestamp is a sequence of characters, denoting the date and/or time.
Syntax of date function
date(format,timestamp)
Parameter | Description |
---|---|
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
PHP Date() – Format the Date
The required format parameter in the date() function specifies how to format the date/time.
Here are some characters that can be used
Parameter | Description |
---|---|
format | Required. Specifies how to return the result:
|
timestamp | Optional. |
Other characters, like”/”, “.”, or “-” can also be inserted between the letters to add additional formatting:
PHP Date() – Adding a Timestamp
The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.
The 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.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst)
Let’s see few examples:
<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d");
?>
It would output like:
2011/06/22
2011.06.22
2011-06-22
Another example using mktime() function
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>
Output would be something like :
Tomorrow is 2011/06/23
We will see more examples related to Date() function in next lesson.