This is an important section as you will certainly need the date() function sooner or later. The date function returns a UNIX timestamp in the format mentioned by you. The syntax of this function is very easy. It's as mentioned below.
date(desired_format, UNIX_timestamp)
desired_format -> The format in which the time
stamp is to be converted into.
UNIX_timestamp -> This is the time elapsed in
seconds since 01-01-1970 00:00:00.
If no timestamp has been specified, the date function returns the current date in the desired format. If the format has also not been specified, a warning is issued.
The following example will make it clear.
<?php
echo date("Y-m-d",60*60*24)." ";
echo date("Y-m-d")." ";
?>
The output of the above code is as given below.
1970-01-02 2008-07-01
We will now learn more about different important formats (Actually formatting options) in the next section.
Calculate future and past dates in PHP
Let us learn how to get dates in future and in the past.
The date in future or in the past can be retrieved by the passing the exact number of seconds since 1st Jan 1970. To do so, first we need to know the current timestamp. In PHP we have inbuilt function time(). Function time(), returns the current UNIX timestamp. Ok so it solves one of our problems. All that's needed now is to add or subtract necessary number of seconds from this timestamp to get a future or past timestamp. How to get the future or past timestamp, is no more a secret now. The following examples show you to retrieve the current or past date in PHP.
1. Dates in Future
For this we retrieve the current timestamp using the PHP function time() and add the number of seconds left to get the future date into it. So, the date next week on today's day will be
date("Y-m-d",time()+60*60*24*7);
The output of the above script is 2008-07-08
Similarly the next year:
date("Y-m-d",time()+60*60*24*365);
The output of the above script is 2009-07-01
2. Dates in the Past
For this we retrieve the current timestamp using the PHP function time() and subtract the number of seconds passed since the date in the past. So, the date in the previous week on today's day will be
date("Y-m-d",time()-60*60*24*7);
The output of the above script is 2008-06-25
Similarly the date exactly one year in the past:
date("Y-m-d",time()-60*60*24*365);
The output of the above script is 2008-07-01
Now, That is not all what PHP date can do. The power of this function lies in returning you the date in whichever format you want. The next section illustrates some important formats.

