IMHO, when you think about comparing time, you automatically start talking about Unix time. In fact, Unix time is the number of seconds that always increases. When you have 2 Unix timestamps, you can use simple arithmetic to understand the difference, and then translate the difference into a human-readable form.
Please note that Unix timestamps are common in almost all programming languages, so you can choose to do this in PHP, MySQL or JavaScript, and everything can work out the same way. I will show you the version of PHP.
So you want to do this:
- Find Unix Timestamp for Target Event
- Save it in a MySQL database
- Unix
- ,
- /, .
PHP:
//Unix timestamp to Dec. 21, 2012 (midnight)
$unix_time = mktime(0,0,0,12,21,2012);
//Connect to MySQL
//"INSERT INTO table (target_timestamp) VALUES ($unix_time);"
//----- user goes to page -----
//Connect to MySQL
$sql = "SELECT target_timestamp FROM table WHERE foo = bar";
$unix_time = do_query($sql);//do_query not defined, assume it gets the timestamp
$current_time = time();
$diff = $unix_time - $current_time
//$diff should be positive and not 0
if( 1 > $diff ){
exit('Target Event Already Passed (or is passing this very instant)');
} else {
$w = $diff / 86400 / 7;
$d = $diff / 86400 % 7;
$h = $diff / 3600 % 24;
$m = $diff / 60 % 60;
$s = $diff % 60;
return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!"
}