First off, here is our function to do the comparison. It will output the number of years, weeks, days, hours, minutes and seconds between two dates.
function calcdiff($starttime, $endtime) {
// Define our global variables that we are going to return from the function
global $years, $weeks, $hours, $days, $minutes, $seconds;
// If the $starttime is later than the $endtime, we're going to flip the values for our calculations
IF($starttime > $endtime) {
$temptime = $starttime;
$starttime = $endtime;
$endtime = $temptime;
unset($temptime);
}
// Define our calculation variables
$minsec = 60;
$hoursec = 3600;
$daysec = 86400;
$weeksec = 604800;
$yearsec = 31536000;
// Calculate the difference between the nowtime and thentime
$difference = ($endtime-$starttime);
// First, determine the number of years if needed
IF($difference >= $yearsec){
$years = floor($difference/$yearsec);
$difference = ($difference%$yearsec);
} else {
$years = 0;
}
// Second, determine the number of weeks if needed
IF($difference >= $weeksec){
$weeks = floor($difference/$weeksec);
$difference = ($difference%$weeksec);
} else {
$weeks = 0;
}
// Third, determine the number of days if needed
IF($difference >= $daysec){
$days = floor($difference/$daysec);
$difference = ($difference%$daysec);
} else {
$days = 0;
}
// Fourth, determine the number of hours if needed
IF($difference >= $hoursec){
$hours = floor($difference/$hoursec);
$difference = ($difference%$hoursec);
} else {
$hours = 0;
}
// Fifth, determine the number of minutes if needed
IF($difference >= $minsec){
$minutes = floor($difference/$minsec);
$difference = ($difference%$minsec);
} else {
$minutes = 0;
}
// Lastly, the remaining difference is the seconds
$seconds = $difference;
// OK, now return all of our values so we can use them later
return $years;
return $weeks;
return $days;
return $hours;
return $minutes;
return $seconds;
}
OK, here is our query, and output.
$endtime = strtotime("2004-10-03 04:45:00 AM"); // This is our comparison date and time. (which the UNIX Timestamp for is 1096793100)
$qry = mysql_query("SELECT * FROM TestTime");
WHILE($row = mysql_fetch_array($qry)){
// Pull out your variables and manipulate them.
$name = $row['Name'];
$starttime = $row['TimeIn'];
// OK, we're going to change things around here. We're going to find the difference between the example times from your earlier post, and the date and time defined by the $endtime variable above. All of the work is done inside of the function above.
calcdiff($starttime, $endtime);
// Now we will echo them to make sure it worked.
echo $name." - ";
echo "There are ";
echo $years." year(s), ";
echo $weeks." week(s), ";
echo $days." day(s), ";
echo $hours." hour(s), ";
echo $minutes." minute(s) ";
echo "and ".$seconds." second(s) ";
echo "between <B>".date("l, F jS Y h:i:s A", $starttime)."</B> and <B>".date("l, F jS Y h:i:s A", $endtime)."</B><BR />";
//Output: (based on the sample data you provided)
// Allen - There are 0 year(s), 12 week(s), 3 day(s), 23 hour(s), 45 minute(s) and 0 second(s) between Wednesday, July 7th 2004 05:00:00 AM and Sunday, October 3rd 2004 04:45:00 AM
// Allen - There are 0 year(s), 12 week(s), 5 day(s), 22 hour(s), 45 minute(s) and 0 second(s) between Monday, July 5th 2004 06:00:00 AM and Sunday, October 3rd 2004 04:45:00 AM
// Allen - There are 0 year(s), 12 week(s), 6 day(s), 23 hour(s), 45 minute(s) and 0 second(s) between Sunday, July 4th 2004 05:00:00 AM and Sunday, October 3rd 2004 04:45:00 AM
// Horn - There are 0 year(s), 12 week(s), 3 day(s), 20 hour(s), 45 minute(s) and 0 second(s) between Wednesday, July 7th 2004 08:00:00 AM and Sunday, October 3rd 2004 04:45:00 AM
// Horn - There are 0 year(s), 12 week(s), 5 day(s), 22 hour(s), 45 minute(s) and 0 second(s) between Monday, July 5th 2004 06:00:00 AM and Sunday, October 3rd 2004 04:45:00 AM
// Horn - There are 0 year(s), 12 week(s), 6 day(s), 22 hour(s), 45 minute(s) and 0 second(s) between Sunday, July 4th 2004 06:00:00 AM and Sunday, October 3rd 2004 04:45:00 AM
// Now we'll close the WHILE loop.
}
Let me know if you need any help with this...