PHP Date/Time Functions
The time() and date() functions are the most used date/time functions in PHP.The time() function returns the current time as a unix timestamp.The date function can be then used to format this value to be human friendly.This is shown in the below example.
$CurrentTime=time();
echo(date("F d Y",$CurrentTime));
?>
Output: January 1 2009
Another time function used is mktime().It is similar to the time() function except that it outputs a specified date and time and not the current time.In effect it artificially generates a timestamp based on inputed date and time.
The format for mktime() is
mktime ( hour, minute, second, month, day, year, is_dst).
hour: Specifies the hour
minute:Specifies the minute
second : Specifies the second
month :Specifies the month
day: Specifies the day
year: Specifies the year
is_dst: =1 if the time is during daylight savings time (DST),
=0 if it is not,
=-1 default value if DST is unknown
<?php echo(date("M-d-Y",mktime(0,0,0,12,32,2008)));
?>
The output is: Jan-1-2009
The date() function formats the date into a human readable format.
The syntax for the date() function is
date(format,timestamp);
The format parameter is used to specify the format in which you want to see the date.
The timestamp is optional.The current timestamp is taken if this parameter is not passed.
The format is specified by using different letters.Some of the widely used letters are
d-the day of the month in numeric
m-Month in numeric
Y-4 digit Year representation
<?php
echo date("Y-m-d");
echo date("D M d, Y");
?>
Output:
2009-06-17
Wed Jun 17,2009
Using the timestamp parameter,we can find dates either using the strtotime() function or the mktime() function.An example for this is if we want to find yesterday’s date using today’s date.
echo "Yesterday's Date: ".date("Y-m-d", strtotime("-1 days"));
?>
Output: 2009-06-16
Related posts:








Leave your response!