These slides explores php date and time library. You will find, what is UNIX time stamp, how to use php's date functions. A beginner introduction by programmer blog
1. Date & Time in PHP
By Programmer Blog – http://programmerblog.net
2. Date & Time – Unix Timestamp
By Programmer Blog – http://programmerblog.net/
The date/time functions allow you to extract and format the date and time on the server.
Note: These functions depend on the locale settings of the server!
Suppose you were tasked with calculating the difference in days between two points in time, but the dates
were provided in the formats July 4, 2005 3:45pm and 7th of December, 2005 18:17 What you would need
is a standard format, some sort of agreement regarding how all dates and times will be presented.
Preferably, the information would be provided in some sort of numerical format, 20050704154500 and
20051207181700, for example. Date and time values formatted in such a manner are commonly referred to
as timestamps.
The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch
on January 1st, 1970. Therefore, the unix time stamp is merely the number of seconds between a particular
date and the Unix Epoch.
This is very useful to computer systems for tracking and sorting dated information in dynamic and
distributed applications both online and client side.
3. Date & Time – Unix Timestamp
By Programmer Blog – http://programmerblog.net/
The Unix timestamp is widely used in PHP. It is the amount of seconds between January 1st 1970 00:00:00
(Unix Epoch) and the present time, to the closest second.
The Unix epoch is the time 00:00:00 UTC on 1 January 1970 (or 1970-01-01T00:00:00Z ISO 8601).
Even this improved situation has its problems. For instance, this proposed solution still doesn’t resolve
challenges presented by time zones, matters pertinent to time adjustment due to daylight savings, or cultural date
format variances.
January 19, 2038 Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of
applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which
will buy the time stamp a "bit" more time.
4. Date & Time PHP
By Programmer Blog – http://programmerblog.net/
Even the simplest of PHP applications often involve at least a few of PHP’s date- and time-related functions
checkdate()
boolean checkdate (int month, int day, int year)
the checkdate() function accomplishes the task of validating dates quite nicely, returning TRUE if the date
specified by month, day, and year is valid, and FALSE otherwise.
echo checkdate(4, 31, 2005);
// returns false
echo checkdate(03, 29, 2004);
// returns true, because 2004 was a leap yearf
echo checkdate(03, 29, 2005);
// returns false, because 2005 is not a leap year
date()
string date (string format [, int timestamp])
The date() function returns a string representation of the present time and/or date formatted
according to the instructions specified by format.
5. Date & Time
By Programmer Blog – http://programmerblog.net/
The date() Function’s Format Parameters
a Lowercase ante meridiem and post meridiem am or pm
A Uppercase ante meridiem and post meridiem AM or PM
d Day of the month, with leading zero 01 to 31
D Three-letter text representation of day Mon through Sun
F Complete text representation of month January through December
h 12-hour format of hour, with zeros 01 through 24
H 24-hour format, with zeros 01 through 24
i Minutes, with zeros 01 through 60
m Numeric representation of month, with zeros 01 through 12
M Three-letter text representation of month Jan through Dec
s Seconds, with zeros 01 through 59
U Seconds since Unix epoch 1114646885
Y Four-digit representation of year
6. Date & Time
By Programmer Blog – http://programmerblog.net/
echo "Today is ".date("F d, Y");
echo "Today is ".date("l");
$weekday = date("l");
$daynumber = date("dS");
$monthyear = date("F Y");
printf("Today is %s the %s day of %s", $weekday, $daynumber, $monthyear);
7. Working with Time
By Programmer Blog – http://programmerblog.net/
echo date("m-d-Y");
Working with Time
The date() function can also produce time-related values. Let’s run through a few examples,
starting with simply outputting the present time:
echo "The time is ".date("h:i:s");
echo "The time is ".date("h:i:sa");
getdate()
array getdate ([int timestamp]) // getdate(1114284300)
gettimeofday()
mixed gettimeofday ([bool return_float])
8. Working with Time
By Programmer Blog – http://programmerblog.net
mktime()
int mktime ([int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])
The mktime() function is useful for producing a timestamp, in seconds, between the Unix
epoch and a given date and time.
echo mktime(20,50,00,4,27,2005);
$now = mktime();
$taxday = mktime(0,0,0,4,15,2006);
// Difference in seconds
$difference = $taxday - $now;
9. Date & Time
By Programmer Blog – http://programmerblog.net/
time()
int time()
The time() function is useful for retrieving the present Unix timestamp.
echo date("F d, Y h:i:s", 1114284300);
setlocale()
It’s possible to specify a locale for a particular classification of data. Six different categories are supported:
strftime()
string strftime (string format [, int timestamp])
The strftime() function formats a date and time according to the localization setting as specified by
setlocale().
%B Complete month name January, February
%d Numerical day of month, with leading zero 01, 15, 26
%m Numerical month, with leading zero 01 through 12
%Y Numerical year, with century 2005
10. Date & Time
By Programmer Blog – http://programmerblog.net
getlastmod()
int getlastmod()
The getlastmod() function returns the value of the page’s Last-Modified header, or FALSE in the case of an
error.
$lastmod = date("F d, Y h:i:sa", getlastmod());
echo "Page last modified on $lastmod";
11. Date & Time
By Programmer Blog – http://programmerblog.net
Thanks for viewing this slide on php date and time functions
Please visit our blog for more detailed tutorials:
http://programmerblog.net/
Subscribe to our newsletter to stay updated on upcoming articles and tutorials