PDA

View Full Version : Help with dates


jsell
10-14-2002, 07:56 PM
can anyone tell Me what code i need to use to subract two dates and come up with the number of day's hours minutes sec's in between?? i can;t seem to get anything to work

thanks in advance

--jsell

Velocd
10-15-2002, 04:18 AM
Let's say your two variables containing the seperate dates, which would be retrieved from a database query, are called $date1 and $date2. We will assume $date1 to be the more recent date.

Here is a basic way:


$difference = round($date1 - $date2);
$result= date('z \d\a\y\s, g \h\o\u\r\s, i \m\i\n\u\t\e\s, s \s\e\c\o\n\d\s',$difference);


This will output the following:

Z days, G hours, I minutes, S seconds


With the variables filled in appropriately.


Here is with months included:


$difference = round($date1 - $date2);
$result= date('n \m\o\n\t\h\s, j \d\a\y\s, g \h\o\u\r\s, i \m\i\n\u\t\e\s, s \s\e\c\o\n\d\s',$difference);



And here is a more flexible way giving you to do whatever with each time variable:


$difference = round($date1 - $date2);
$seconds= date('s',$difference);
$minutes= date('i',$difference);
$hours= date('g',$difference);
$days= date('j',$difference); <-- note: days of month
$days = date('z',$difference); <-- note: days in year
$months= date('n',$difference);


Hope any of this helps ;)

jsell
10-15-2002, 02:09 PM
Thanks Velocd! :) that gave exactly what Ineeded! I appriciate your help

--jsell