Datetime Functions in PHP


Date functions allow you to display, format and manipulate the date and time on the server.
Date functions are explained in detail below:

(1) date:

date () function allows you to format and display current date and time of the web server.
Syntax:
date (Format, Timestamp)
Here,
Format
specify how you would like to display the date and time.
Timestamp indicates UNIX Timestamp. It is optional.
You can use following letters to format display of date and time in the format parameter.

Character Purpose Example
d Represents the day of the month in numeric format with leading zero. 01 to 31
D Represents the day of month in text format with three letters of day. Mon, Tue etc…
j Represents the day of the month in numeric format without leading zero. 1 to 31
l   Represents the day of month in text format with all letters of the day. Monday, Tuesday etc…
m Represents the month of the year in numeric format with leading zero. 01 to 12
M Represents the month of the year in text format with three letters of Month. Jan, Feb etc…
F Represents the month of the year in text format with all letters of Month. January, February, etc…
n Represents the month of the year in numeric format without leading zero. 1 to 12
y Represents year in two digits. 01, 02, etc…
Y Represents year in four digits. 2001, 2002, etc…
a Represents am or pm in Lowercase. am or pm
A Represents am or pm in Uppercase. AM or PM
g Represents the hour of time in 12-hour format without leading zero. 1 to 12
G Represents the hour of time in 24-hour format without leading zero. 0 to 23
h Represents the hour of time in 12-hour format with leading zero. 01 to 12
H Represents the hour of time in 24-hour format with leading zero. 00 to 23
i Represents the minute of time with leading zero. 00 to 59
s Represents the second of time with leading zero. 00 to 59
     

You can use separator between above characters to separate them from each other so user can easily interpret the date and time.

<?php
echo date("d/m/y")."<br/>";
echo date("D/M/Y")."<br/>";
echo date("l-F-y")."<br/>";
echo date("d/m/y h:i:s a")."<br/>";
echo date("d/m/y h:i:s A")."<br/>";
echo date("d/m/y G:i:s A")."<br/>";
?>
Output:
31/03/11
Thu/Mar/2011
Thursday-March-11
31/03/11 09:33:22 am
31/03/11 09:33:22 AM
31/03/11 9:33:22 AM


(2) getdate:

getdate () function returns an array which contains following information of the current UNIX timestamp: Second, Minute, Hour, mDay (Day of Month), wDay (Day of Week), Year, yDay (Day of Year), WeekDay and Month
Syntax:
getdate (timestamp)
Here,
Timestamp
is an optional argument which represents UNIX timestamp.

<? php
print_r (getdate ());
?>
Output:
Array ( [seconds] => 56 [minutes] => 39 [hours] => 9 [mDay] => 31 [wDay] => 4 [mon] => 3 [year] => 2011 [yDay] => 89 [weekday] => Thursday [month] => March [0] => 1301544596 )


(3) checkdate:

checkdate () function accepts month, day and year of the date and it determines weather the specified date is valid or not.
It returns true value if the specified date is valid otherwise it returns false value.
Syntax:
checkdate (month, day, year)

<?php
if (checkdate(4,2,2011))
{
echo "Date is valid Date<br/>";
}
else
{
echo "Date is not valid<br/>";
}
if (checkdate(2,30,2011))
{
echo "Date is valid Date<br/>";
}
else
{
echo "Date is not valid<br/>";
}
?>
Output:
Date is valid Date
Date is not valid


(4) time:

time () function returns the current UNIX timestamp.
The current UNIX timestamp indicates number of seconds since January 1 1970 00:00:00 GMT.
Syntax:
time ()

<?php
echo time();
?>
Output:
1301545030


(5) mktime:

mktime () function returns the current UNIX timestamp if no parameter is passed to this function. It can also return the current UNIX timestamp for the date and time that is passed as an argument.
Syntax:
mktime (hour, minute, second, month, day, year, dst)
All the parameters mentioned above are optional.

<?php
echo mktime(1,1,1,1,1,2001);
echo mktime(1,1,1,1,1,2001);
?>
Output:
1301546056
978291061

Download Projects


Download Programs