View Full Version : Begginer Date Probs

04-15-2004, 11:07 AM

Ive got to the date section of php in my book im working through but cannot seem to get any of the code to work without either a jumble of random numbers appearing or error messages.

Bellow i have typed in word for word the parts of the book im stuck on.

You can assign a timestamp with the current date and time to a variable with the following statements:
$today = time();

You can store a specific date and time as a timestamp using the function mktim. The Format is:
$importantDate = mktime (h,m,s,mo,d,y)

where h is hours, m is minutes, s is seconds, mo is months, d is days, y is years. For instance you would store
the date January 15 2002 using the following statement:
$importantDate = mktime(0,0,0,1,15,2002);

if you wanted to know how long ago $importantDate was you could subtract it from $today For instance:
$timeSpan = $today - $importantDate;

This gives you the number of seconds between the important date and today or use the statement:
$timeSpan = (($today - $importantDate))/60/60

Dates and times must be formatted in the correct MySQL format to store them in your database. PHP functions can
be used for formatting. For instance, you can format todays date in MySQL format using the statement:
&today = ("yyyy-m-d");

Cheers for any help

04-15-2004, 11:37 AM
well the book is wrong here ....

$timeSpan = (($today - $importantDate))/60/60;
should be
$timeSpan = $today - $importantDate;

to give the time in seconds, time() && mktime() return the number of seconds since the beginning of the unix epoch (Jan 1st 1970), so all date calculations on variables returned from time() or mktime() are already in seconds ... e.g. you would therefore convert to hours via ..

$timeSpanInHours = ($today - $importantDate) * 3600 ;

04-15-2004, 03:32 PM
The Damm book seems to have a lot of errors in it!! i am only up to pg 133 and already ive had tonnes of mistakes! i email the publishers but they ignore me !!!

04-15-2004, 03:50 PM
I honestly find the PHP manual the best 'book' , the user notes are invaluable , you can download the manual (notes and all) in a variety of formats ... (windows CHM recommended) http://www.php.net/download-docs.php