Friday, 5 October 2012

PHP Date Functions

10:36 pm

PHP Date Functions

PHP Date Functions :

The php date() function is used to format a time and/or date.
Date(format [,timestamp])
Where format argument is mandatory and it specifies the format of the timestamp.timestamp attribute is optional ,specifies a timestamp . default is the timestamp of current date and time.
Timestamp is the number of seconds from 1970 jan 1st onwards.
Different formats used for date and time :
    D – day of the week {mon,tue,wed,…etc}
 d- the day of the month(01-31)
m – the current month , as a number(01-12)
M – {jan,feb,mar,,,,,,,,,,,}
y- the current year in 4 digits.
H – hours
i-                    Minuts
s- seconds
S – suffix to date like rd,th,st,nd etc… characters like “/”,”.”,or “-“ can be inserted between the letters to add formatting .
Time() – is a built-in function of php used for getting the seconds from 1970 jan 1st onwards(getting current timestamp) to get the timezone , open php.ini and set as date.timezone = asia/calcutta.
Strtotime(date in string) – function is used to convert a date in string format to timestamp(corresponding number of seconds from 1970 jan 1st).

 $d = date("1,ds-M-y::H:i:s");
 echo $d."<br>";
 $t = time();
 echo $t."seconds from 1970 jan 1st <br>";
 $d = date("1,ds-M-y::H:i:s",366);
 echo $d."<br>";
 $d1 = date("y-m-d");
 $d2 = "2011-09-21";
 $diff = (strtotime($d1)-  strtotime($d2))/(60*60*24);
 echo "days difference=".$diff ;



Post a Comment