Saturday, 5 April 2014

Date Datatype in PHP with Different Date Format.

Date in PHP with Example code.

Date Datatype's syntax is the 

date(format in string, time stamp);

Default format which the developer wants is dd/mm/yyyy we can do it by following code.

 date("d/m/Y");

This code will print the current date with 05/04/2014

Formats a time and date according to the format string provided in the first parameter. If the second parameter is not specified, the current time and date is used.As shown in the above example.

 date("m/d/Y");

This code will print the current date with 04/05/2014

The mktime() function returns the timestamp for the date and we can store the output of the function mktime() to any variable in php for printing suitable date.


Syntax of mktime() is given below

mktime(hour,minute,second,month,day,year,is_dst)

We can change the format of the date by mktime function.By providing the time stamp as a second argument in the date function.
if we want to print the date after 10 days then we can use below formatting.with the help of mktime and date.
hour=0
minite=0
second=0
month = m means current month April (4)
day = d+10 means 10 days will be added to the current date and
year = Y means current year (2014)

$dateten = mktime(0,0,0,date("m"),date("d")+10,date("Y"));
echo "The date after 10 days is ".date("d/m/Y", $dateten);


output : The date after 10 days is 15/04/2014

Here in above example the $dateten variable will print the date after 10 days as output.

Date Format

D
Instead of current date 05 if we have to print name of the day then use above char D.It will print first three letters of the current day.

 date("D/m/Y");

This code will print the current date with SAT/04/2014

l (small l)
Instead of current date 05 if we have to print name of the day then use above char D.It will print name the current day.

 date("l/m/Y");

This code will print the current date with Saturday/04/2014


M
Instead of current Month 04 if we have to print name of the month then use above char F.It will print first three letters of the current month.
 date("d/M/Y");

This code will print the current date with 05/Apr/2014

F
Instead of current Month 04 if we have to print name of the month then use above char F.It will print name of the current month.

 date("d/F/Y");

This code will print the current date with 05/April/2014

y
If we have to print the Current year's last two digits then use y.Year with two digits; e.g., “14”.

date("d/m/y");

This code will print the current date with 05/04/14

PHP PRoject Training in Vadodara Gujarat.
Best PHP training provider in Vadodara.
Visit Website : PHP Training Center Gujarat

2 comments:

  1. Learn PHP Project with MySQL
    visit website : http://www.vataliyatuitionclasses.com

    ReplyDelete
  2. PHP - World's best Web Development Technology visit our official website for more information: http://www.vataliyacomputer.in

    ReplyDelete