PHP Date and Time Date and time Tutorial
What time is it? How many seconds have passed since the UNIX Epoch started? Learn How to use PHP Date and Time functions.
What time is it? How many seconds have passed since the UNIX Epoch started? Learn How to use PHP Date and Time functions.
Date and time
Learn how to use PHP Date and Time functions
To get formatted date and time value you can use
date() function. It is
very easy to pass any format you want to get formatted time output.
string date(string format, int timestamp);
-
format: represents output string format (read below)
-
timestamp: date and time value you want to output, in special
number format. If you omit this argument, function will take current date and
time value.
Here are special meanings of some symbols which you can use in format string
(first parameter)
*
d - day of the month, numeric, 2 digits (with leading zeros)
*
D - day of the week, textual, 3 letters; i.e. "Fri"
*
F - month, textual, long; i.e. "January"
*
h - hour, numeric, 12 hour format
*
H - hour, numeric, 24 hour format
*
i - minutes, numeric
*
j - day of the month, numeric, without leading zeros
*
l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"
*
m - month, numeric
*
M - month, textual, 3 letters; i.e. "Jan"
*
s - seconds, numeric
*
S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
*
Y - year, numeric, 4 digits
*
w - day of the week, numeric, 0 represents Sunday
*
y - year, numeric, 2 digits
*
z - day of the year, numeric; i.e. "299"
You can include any other symbols in
format string and they should be outputted as they are. Only above symbols are
replaced with their time and date values.
Examples:
print(date( "l dS of F Y h:i:s" ));
Output:
Friday 04th of January 1994 3:52:31
print(date("Current time is: H (hours), i (minutes), s (seconds)"));
output:
Current time is: 12 (hours), 34 (minutes), 13 (seconds)
mktime - get timestamp for date
and time value
Syntax:
int mktime (int hour, int minute, int second, int month, int day, int
year);
Timestamp has been created to represent date / time value as a single
integer number. So any logical date and time can be converted and used as
special integer called timestamp. That's how you are able to compare two time
moments as just two integer numbers. This timestamp contains the number of
seconds between the Unix Epoch (January 1 1970) and the time specified. For
example, January first of 1997th year will be less number than January second of
that year.
In databases the date type fields usually stored as timestamp integers. So to
create
query to get all records in specified time period you can use
mktime() function to get the boundary timestamp integers. If you combine
both
date and
mktime function, you will be able to perform any
time operation that you can imagine (and you don't need time machine or
something :) )
Example:
Let's print date that happened 7 days ago:
$m=date('m');
$d=date('d');
$Y=date('Y');
print(date('Y-m-d', mktime(0,0,0, $m, $d-7, $Y)));
Note that
PHP mktime()'s
arguments sequence is different from standart UNIX mktime function. Most right
arguments can be
omitted and they will be set to
current date time
values. ьktime also would make all needed
corrections if after ariphmetic
calculations you would get incorrect date, for example 32nd of Febrary :).
Example. All lines print "
Jan-01-1998".
print(date("M-d-Y", mktime(0,0,0,12,32,1997)));
print(date("M-d-Y", mktime(0,0,0,13,1,1997)));
print(date("M-d-Y", mktime(0,0,0,1,1,1998)));