PHP Date Function
Welcome! PHP has nice built in date function which allows you to display dates in human readable formats.
![]() | Click on a Topic [ Hide ] |
PHP Dates Formats
| Learn PHP and MySQL fast! | ||
|
Learn how you can start building rich interactive database driven sites like a professional developer easily and quickly... >> Order your copy now for just $39.95 << |
|
Displaying dates in different formats.
<?php
echo date("Y-m-d");
echo date("Y/m/d");
echo date("M d, Y");
echo date("F d, Y");
echo date("D M d, Y");
echo date("l F d, Y");
echo date("l F d, Y, h:i:s");
echo date("l F d, Y, h:i A");
?>
Output
2008-07-24
2008/07/24
Jul 24, 2008
July 24, 2008
Thu Jul 24, 2008
Thursday July 24, 2008
Thursday July 24, 2008, 11:22:16
Thursday July 24, 2008, 11:22 AM
PHP Date Function Syntax
date(format, timestamp)
As seen above, the php date function take two arguments.
- format - Always required. Specify the format to display the in.
- timestamp - Optional. Specify UNIX time stamp. If not passed, the current timestamp is used.
PHP Date Function Parameters
format - The first parameter, format, in the date function specifies how to display the date/time. It uses different letters to represent the date and time. Some of the letters used above are described here.
- d - The day of the month, i.e. 01-31
- m - Month representation in numbers, i.e. 01-12
- Y - Year in four digits
For complete list of date/time format reference, click here.
timestamp - The second parameter, timestamp, is an optional parameter. Timestamp is the number of seconds since January 1, 1970 at 00:00:00 GMT. This is also known as the Unix Timestamp.
PHP Date: Finding Date / Time
Using the 2nd timestamp parameter we can do things like say find exactly what date or day it was yesterday or a week ago or what date it will be 1 month from today.
There are two ways we can do that.
- Using the PHP strtotime function.
- Using the PHP mktime function.
- strtotime - Convert any English textual datetime description into a Unix timestamp.
- mktime - Get Unix timestamp for a date.
PHP Date: Using strtotime to find date/time
Let's see some of the examples to find out dates using date and strtotime function.
Find Yesterday’s date
<?php
echo "yesterday was ".date("Y-m-d", strtotime("-1 days"));
?>
Output
yesterday was 2008-07-23
Find Date one week ago
<?php
echo "1 week form today was ".date("Y-m-d", strtotime("-1 weeks"));
?>
Output
1 week form today was 2008-07-17
Find Date one month after
<?php
echo "1 month from today will be ".date("Y-m-d", strtotime("+1 months"));
?>
Output
1 month form today will be 2008-08-24
PHP Date: Using mktime to find date/time
mktime could be used to find more specific things like find the next leap year in the calendar.
Find Leap Year
<?php
$day = "";
/*
* since leap year falls ever 4 years so loop for 4 times
*/
for($i=0; $i<4; $i++)
{
//get day timestamp for feburary 29 for this year
$day = date("d", mktime(0, 0, 0, 2, 29, date("Y")+$i));
/*
* check if day equals 29.
* If day is 29 then it must be the leap year. if day is 01, then it not a leap year.
*/
if($day == 29)
{
$year = date("Y")+$i;
break;
}
}
echo "next leap year is in year $year";
?>
Output
next leap year is in year 2008
The mktime takes 6 arguments. The parameters are explained as below.
- hour - The number of the hour.
- minute - The number of the minute.
- second - The number of seconds past the minute.
- month - The number of the month.
- day - The number of the day.
- year - The number of year.
From HTML to PHP programming...
Learn how to build websites in PHP
| << PHP Email | Top | PHP Cookies >> |




