How to Display Previous Date or Next Date Using
PHP? it is very simple, just use date() and strtotime(). Using the same
way, we can display "last month" and "next month", or "last year" or
"next year".
Display Date in PHP?
How to display date using PHP? 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 this:
date("F j, Y");
In this example, "F" represents a full textual representation of
a month, such as January or March; "j" means day of the month without
leading zeros, such as 1 or 8; and "Y" is for a full numeric representation
of a year, 4 digits, for examples: 1999 or 2003.
So when you have:
echo date("F j, Y");
The output will be something like:
September 25, 2008
Or if you want to have the output like "9/25/2008", change the above
code to:
date("n/j/Y");
Here is a list the formatting options for date September 3, 2008:
| Code |
Output |
| date("F j, Y I"); |
September 3, 2008 Wednesday |
| date("M j, Y D"); |
Sep 3, 2008 Wed |
| date("n/j/Y"); |
9/3/2008 |
| date("m/d/Y"); |
09/03/2008 |
| date("Y-m-d"); |
2008-09-03 |
| date("m/d/y"); |
09/03/08 |
As you can see, you can change the position of the format character to modify
the output format. For example:
date("m-d-Y");
and
date("Y-m-d");
use the same characters, but the positions are different. So the output strings
are different:
09-03-2008
2008-09-03
|