Page 1 of 1

Time Left

Posted: Tue May 19, 2009 8:56 pm
by bholaday
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.

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";
 
 

Re: Time Left

Posted: Tue May 19, 2009 10:36 pm
by bholaday
SOLVED

I figured out that I needed to be using Unix time to do calculations.

Here is the code that worked:

Code: Select all

 
//now
$now = time();
 
//convert DateTime format to Unix timestamp
//credit: http://www.webdeveloper.com/forum/archi ... 62042.html
list($date, $time) = explode(' ', $row['Received']); //$row['Received'] is my stored start time in the database
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
 
//create deadline from startTime plus 3 days
$deadline = mktime($hour, $minute, $second, $month, $day+3, $year);
 
//calculations
//credit: http://www.phpf1.com/tutorial/php-date-difference.html
$dateDiff = $deadline - $now;
$fullDays = floor($dateDiff/(60*60*24));
$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
 
echo $now . " now<br />";
echo $deadline . " deadline<br />";
echo $dateDiff . " dateDiff<br />";
echo $fullDays . " fullDays<br />";
echo $fullHours . " fullHours<br />";
echo $fullMinutes . " fullMinutes<br />";