Time Left
Posted: Tue May 19, 2009 8:56 pm
Hi,
I'm writing a little snippet of code to output the amount of time that a user has left to perform an action. The user has three days from a stored DateTime entry in a database.
I'm not very familiar with DateTime and date functions in php, so I am having some trouble with this. I've tried a number of different methods, but I think my lack of knowledge with these functions and objects isn't helping.
I'd like an output like: You have 2 Days, 3 Hours, 45 minutes, and 16 seconds left.
Can anyone help me on this? Thanks!
Here is my code, but it's not very organized since I've been trying lots of different ways to do this.
I'm writing a little snippet of code to output the amount of time that a user has left to perform an action. The user has three days from a stored DateTime entry in a database.
I'm not very familiar with DateTime and date functions in php, so I am having some trouble with this. I've tried a number of different methods, but I think my lack of knowledge with these functions and objects isn't helping.
I'd like an output like: You have 2 Days, 3 Hours, 45 minutes, and 16 seconds left.
Can anyone help me on this? Thanks!
Here is my code, but it's not very organized since I've been trying lots of different ways to do this.
Code: Select all
//now
$now = date("Y-m-d H:i:s");
//received date (already in the above format)
$startTime = $row['Received']; //retrieved from MySQL database (DateTime format)
//add three days
$deadline = strtotime("+3 day", strtotime($startTime));
$deadline2 = date("Y-m-d H:i:s", $deadline); //format
//do calculations
$timeleft = $deadline - $now;
$timeleft2 = date("Y-m-d H:i:s", $timeleft); //format
// break these dates into their constituent parts
$gd_a = getdate( strtotime($row['Received']) );
$gd_b = getdate( strtotime($now) );
// Now recreate these timestamps
$a_new = mktime( $gd_a['hours'], $gd_a['minutes'], $gd_a['seconds'], $gd_a['mon'], $gd_a['mday']+3, $gd_a['year'] );
$b_new = mktime( $gd_b['hours'], $gd_b['minutes'], $gd_b['seconds'], $gd_b['mon'], $gd_b['mday'], $gd_b['year'] );
echo $b_new . "<br />";
echo date("Y-m-d H:i:s", $b_new) . "<br />";
// Subtract these two numbers and divide by the number of seconds in a day.
$myTimeDays = round( abs( $a_new - $b_new ) / 86400 );
$myTimeMinutes = round( abs( $a_new - $b_new ) / 3600 );
$myTimeSeconds = round( abs( $a_new - $b_new ) / 60 );
$myTime = $a_new - $b_new;
$myTime = date("Y-m-d H:i:s", $myTime);
echo $now . " now<br />";
echo $startTime . " deadline<br />";
echo $deadline2 . " deadline + 3<br />";
echo $timeleft2 . " time left<br />";
echo $myTimeDays . " myTimeDays<br />";
echo $myTimeMinutes . " myTimeMinutes<br />";
echo $myTimeSeconds . " myTimeSeconds<br />";
echo $myTime . " myTime<br />";
//$timeLeft = ($row['Received'] + 3) - $now;
//echo $timeLeft . "<br />";
//$daysleft = round((($timeleft/24)/60)/60);
//echo $daysLeft . "<br />";
echo "Name: " . $row["FirstName"]." ".$row["LastName"]."\n";
echo "Status: ".$row['Status']."\n";
echo "Target: ".$row['CurrentTarget']."\n";
echo "Time Left: ".$timeLeft."\n";
echo "Days Left: ".$daysLeft."\n";