Posted by : senan Wednesday, February 12, 2014

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

PHP Dates Formats

 

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-02-12
2014/02/12
Feb 12, 2014
February 12, 2014
Wed Feb 12, 2014
Wednesday February 12, 2014
Wednesday February 12, 2014, 02:15:12
Wednesday February 12, 2014, 02:15 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.
  • 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.
  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-02-11

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-02-05

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-03-12
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.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Learning Programming Language - Skyblue - Powered by Blogger - Designed by Johanes Djogan -