Date() handling in php

The php date() function is used to show dates and time.

The date() function takes two parameters, one is required and second one is optional.

Parameters of date()
  1. format. Its Required.Specify the format in which timestamp or date should be display.
  2. timestamp. Its Optional. It specifies the timestamp. Default timestamp is the current date and time.

Timestamp????

A timestamp is the numbers of seconds since jan 1,1970 at 00:00:00 gmt. It is also called Unix Timestamp.

How to format date using date()

The first parameter in the date() tells the format of the date to be displayed. It use different letters to represent date and time formats. Some are given below:

  1. d- The day of the month (01-31)
  2. m- The current month in the form of number (01-12)
  3. Y- The current year in the form of four digits like 2009

You can characters like "-". "/" between the letters to make dates and time stamp more readable.

Examples:-

echo date("Y-m-d");

echo date("Y/m/d");

Adding a Timestamp

The second parameter that used in the date() represents a timestamp. Its optional, if it is missing then current time will be used.

we normally use mktime() to create timestamp for future dates like tommorow or day after tommorow and so, on.

the mktime() function return the unix timestamp for the date specified.

To see what is date at tomorrow.

$date_tommorow=mktime(0,0,0,date("m"),date("d")+1,date("Y"));

echo "The date for tommorow is".$date_tommorow;

There are lot of functions that deals with dates and time in php, i will tell u about these in my post later.

0 comments: