Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Display Date in PHP (0 Comments)
Admin: Posted Date: March 3, 2010

This tutorial will show you how to do it. To display date, we use function date(). But you need to format it by choose the formatting options. For example, if you want to display a date like "September 25, 2008", you need to write the PHP code like.

Display Previous Date or Next Date Using PHP

As you may know, to display current date, we can use:

date("F j, Y");

Let's say, if today is September 25, 2008, then:

echo date("F j, Y");

will output:

September 25, 2008

You may ask: how to display yesterday's date, for example, September 24, 2008? In PHP, it is very simple, just use code like this:

$today=date("F j, Y");
$yesterday=date ("F j, Y", strtotime ( "$today - 1 day" ) );
echo $yesterday;

The output will be something like:

September 24, 2008

Similarly, to display the next day, we use:

$today=date("F j, Y");
$tomorrow=date ("F j, Y", strtotime ( "$today + 1 day" ) );
echo $tomorrow;

The output will be something like:

September 26, 2008

Using the same way, we can display "last month" and "next month", or "last year" or "next year":

$thismonth=date("F");
$lastmonth=date ("F", strtotime ( "previous month" ) );
$nextmonth=date ("F", strtotime ( "next month" ) );

$thisyear=date("Y");
$lastyear=date ("Y", strtotime ( "previous year" ) );
$nextyear=date ("Y", strtotime ( "next year" ) );

echo $lastmonth . '<br>';
echo $nextmonth . '<br>';

echo $lastyear . '<br>';
echo $nextyear . '<br>';

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: