Date Handling in PHP

PHP Date: date()

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.


Date formats in PHP

A few of the important date formats has been mentioned here. These date formats should satisfy almost all your basic needs. However, You can visit http://www.php.net to learn all about different date formats and which suits you the most.

Format Description Example based on current time.
d Returns the current day 01
D Returns the name of the day Tue
F Full month name July
m Month number 07
Y A four digit year 2008
y A two digit year 08
H Hour in 24 hr format 19
m minutes 07
s seconds 47
c Complete date c

You can use optional separator in between to get your time in desired legible format. The two examples below display the current date. One without separator and other with separator.

<?php
echo "Current time is - ". date("dmYHms");

echo "<br>Current time is -". date("d-m-Y H:m:s");
?>

The output of the above script is as below.

Current time is - 01072008190747
Current time is -01-07-2008 19:07:47

Note:- The date on which the expressions on this page were evaluated was 01st July 2008.