PHP Learn It! PHP Date Function
"Best Deal"

"Bored - Do You Agree?"
t-shirtgirlspics

Bored - Studied Hard - Enjoy Yourself "TAKE A BREAK"

"Decide Now-So Many Choices"
"Amazing Deals"
Once You Know, You Newegg

PHP Date Function

Welcome! PHP has nice built in date function which allows you to display dates in human readable formats.

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

2014-04-18
2014/04/18
Apr 18, 2014
April 18, 2014
Fri Apr 18, 2014
Friday April 18, 2014
Friday April 18, 2014, 03:44:23
Friday April 18, 2014, 03:44 PM

PHP Date Function Syntax

date(format, timestamp)

As seen above, the php date function take two arguments.

  1. format - Always required. Specify the format to display the in.
  2. 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.

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.

  1. strtotime - Convert any English textual datetime description into a Unix timestamp.
  2. 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 2014-04-17

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 2014-04-11

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 2014-05-18

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 2016

The mktime takes 6 arguments. The parameters are explained as below.

  1. hour - The number of the hour.
  2. minute - The number of the minute.
  3. second - The number of seconds past the minute.
  4. month - The number of the month.
  5. day - The number of the day.
  6. year - The number of year.

From HTML to PHP programming...
Learn how to build websites in PHP

<< PHP Email Top PHP Cookies >>