Please send questions to
st10@humboldt.edu .
* playing with dates and times in PHP
* times/dates have "pieces", as we perceive them ---
a month, a day, a year, an hour, a minute, a second, etc.
* to SIMPLIFY things, there is a programming convention
that tries to treat a time and date as a SINGLE value:
the number of seconds that have elapsed since midnight on
January 1, 1970
* Why January 1, 1970? pretty much arbitrary...
* (See Wikipedia entry on "Unix time" for more on
this, if interested...)
* FORTUNATELY - PHP provides a number of functions to
make this less nuts than it sounds...
* These number of seconds since midnight on 1-1-70 are called
epoch timestamps...
(at least, that's what "Learning PHP 5" by David Sklar calls 'em...)
* SO: let's start with just displaying the current date and time:
* the date function takes 1 or 2 arguments:
* 1st (required) is a string format,
* 2nd (optional) is an epoch timestamp, assumed to be
the timestamp for the current time if no 2nd argument is given
* strftime is another, similar function, that also takes 1 or 2
analogous arguments:
* 1st (required) is a string format,
* 2nd (optional) is an epoch timestamp, assumed to be
the timestamp for the current time if no 2nd argument is given
* date: 'r' gives a default, raw format,
strftime: '%c' gives a complete default depiction
* a first example: datetime1.php
* So, sticking with the current time for now,
what are some of the formatting options for date and strftime?
* with date, certain letters in the format string get
replaced with particular values in the resulting string;
* with strftime, %letter values get replaced with particular
values in the resulting string;
* so, looking at date:
m - the month as a number (1-based)
d - the day of the month as a number (1-based)
y - the two-digit year
(note: characters that aren't "special" are simply left as-is
in the resulting string ....)
date('m/d/y') -> '11/28/06'
date('d.m.y') -> '28.11.06'
for more examples, see:
http://us3.php.net/date
http://www.w3schools.com/php/func_date_date.asp
* see datetime2.php
* and NOTE - what if you want text in your date result string,
that happens to have special format-meaning?
... need to concatenate it with the date-results, or some
such --- you see that in datetime2.php, also.
* for strftime:
%m - the month as a number
%d - the day of the month as a number
%y - the two-digit year
for more examples, see:
http://us3.php.net/strftime
* see datetime2.php also
* BUT - everything is not "right now"!
* we need to talk about that 2nd argument - that timestamp
* FIRST: time( ) returns the current epoch timestamp,
which you can then add or subtract seconds to accordingly;
* add 1? 1 second later
1 minute later? + 60
1 hour later? + (60*60)
1 day later? + (24*60*60)
7 days later? + (7*24*60*60)
* this computation can then be the 2nd argument to date() or
strftime()... as you can see in example datetime3.php
* AND: what if I want the epoch timestamp for a given date/time?
* mktime() is a PHP function that takes
hour, minute, second, month, day, and year
and returns the corresponding epoch timestamp:
mktime(13, 30, 45, 10, 20, 1982)
--> return the epoch timestamp for 1:30 pm and 45 seconds on Oct. 20, 1982
* and if you leave off one or more of the arguments, they default
to the current date or time
* quick discussion of the date vs. strftime:
* According to D. Sklar in "Learning PHP 5", Ch. 9,
"The strftime() PHP function relies on an underlying
operating system function (also called strftime()).
That's why some format characters aren't supported on Windows."
On the other hand, date() is PHP-specific - and so ought
to work the same everywhere.
* strftime is more convenient if you want to blend some
additional text with the date and time information
* looking at their format options, each does have some interesting
variations...
* AND: strftime() lets you give month, day, etc. IN THE CUURENT
LOCALE - would this be more internationally-friendly??
* setlocale() is supposed to let you set the desired
locale...
* strtotime - a rather odd function that excepts a rather-English-y
string and a timestamp, and returns the corresponding relative
timestamp
$now = time();
$later = strtotime('Thursday', $now);
$before = strtotime('last thursday', $now);
print strftime("now: %c <br>\n", $now);
print strftime("later: %c <br>\n", $later);
print strftime("before: %c <br>\n", $before);
* example: datetime5.php
* http://us3.php.net/strtotime for more on the legal string
arguments to this
* in INPUTTING information from users...
* use <select> drop-downs;
* let users enter day, month, year separately, as separate
drop-downs;
* likewise for time!