Monday 22 April 2013

PHP Date


PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.


ü While PHP's date() function may seem to have an overwhelming amount of options available, isn't it always better to have more choices than not enough?
ü With PHP's date function you format timestamps, so they are more human readable.
ü The date function uses letters of the alphabet to represent various parts of a typical date and time format. The letters we will be using in our first example are:

     d: The day of the month. The type of output you can expect is 01 through 31.
     m: The current month, as a number. You can expect 01               through 12.
     y: The current year in two digits ##. You can expect 00 through 99.

ü We'll tell you the rest of the options later, but for now let's use those above letters to format a simple date!
ü The letters that PHP uses to represent parts of date and time will automatically be converted by PHP.

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.
The mktime() function returns the Unix timestamp for a date.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax for mktime():


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


ü To go one day in the future we simply add one to the day argument of mktime():

Example:

<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>

Display:

Tomorrow is 2009/05/12


  
Example:

<?php
echo date("Y/m/d") . "<br>";
echo date("Y.m.d") . "<br>";
echo date("Y-m-d");
?>

Output:

2009/05/11
2009.05.11
2009-05-11

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More