Glen Posted October 24, 2009 Share Posted October 24, 2009 Hi everyone, PHP times and date are not my strongest part of php Now I am making a countdown script I know though it want be live though I am not worried about that right now. <?php //view attack page //set the username $user = $_SESSION['username']; //connect to database include("connect_attackplanner.php"); //Query the database $result = mysql_query("SELECT * FROM `" . $user . "_attacks`"); $num_rows = mysql_num_rows($result); //Display message if you have no attacks cheduled if($num_rows == 0) { die ("You have no attacks planned!"); } //start table echo "<table class = \"box\"><tr> <th>Enemy Village</th> <th>Attacking Coords</th> <th>Launch Time</th> <th>Arrival Time</th> <th>Unit</th><th>Countdown to Attack</th></tr>"; //get results while($row = mysql_fetch_array($result)) { $enemycoords = $row['target_village']; $attackcoords = $row['attacking_village']; $launchtime = date("d/m/Y H:i:s",$row['launch_time']); $arrivaltime = date("d/m/Y H:i:s",$row['land_time']); $arrivaltime1 = $row['land_time']; $unit = $row['unit']; echo " <tr><td>$enemycoords</td> <td>$attackcoords</td> <td>$launchtime</td> <td>$arrivaltime</td> <td>$unit</td>"; //create countdown script $today = time () ; $difference =($arrivaltime1-$today) ; //the rest of the code goes in here } //finish table echo "</table>" ?> I need to convert $difference into hh:mm:ss though I have no idea how to go about it. I know that things will have to be multiplied and that date() does not work. If you have any idea's aswell as explaining the code as I need to expand my PHP date/time knowledge Many Thanks -Glen Link to comment https://forums.phpfreaks.com/topic/178865-solved-convert-difference-timestamp-to-hhmmss/ Share on other sites More sharing options...
cags Posted October 24, 2009 Share Posted October 24, 2009 To convert a timestamp to a datetime format you simply use the date function. $time = date("h:i:s", $difference); Link to comment https://forums.phpfreaks.com/topic/178865-solved-convert-difference-timestamp-to-hhmmss/#findComment-943633 Share on other sites More sharing options...
Glen Posted October 24, 2009 Author Share Posted October 24, 2009 No that is not what I want. I don't think I have explained it properly. So if I have a target time 18/12/2009 23:00:00 and I have today's time 24/10/09 21:55:00 I want to now how many hours:mins:sec there is in difference. -Glen Link to comment https://forums.phpfreaks.com/topic/178865-solved-convert-difference-timestamp-to-hhmmss/#findComment-943639 Share on other sites More sharing options...
cags Posted October 24, 2009 Share Posted October 24, 2009 What format are launch_time and land_time in the database? Link to comment https://forums.phpfreaks.com/topic/178865-solved-convert-difference-timestamp-to-hhmmss/#findComment-943642 Share on other sites More sharing options...
Glen Posted October 24, 2009 Author Share Posted October 24, 2009 What format are launch_time and land_time in the database? They are timestamps. Link to comment https://forums.phpfreaks.com/topic/178865-solved-convert-difference-timestamp-to-hhmmss/#findComment-943643 Share on other sites More sharing options...
cags Posted October 24, 2009 Share Posted October 24, 2009 Well in that case you can work out the difference between the two in seconds using... $difference = strtotime($row['land_time']) - strtotime($row['launch_time']); Then divide that number by the appropriate amounts to find the number of hours, minutes and seconds. Not tested but something along the lines of... $hours = floor($difference / 3600); $minutes = floor(($difference % 3600) / 60); $seconds = floor($difference % 60); echo "{$hours}:{$minutes}:{$seconds}"; Link to comment https://forums.phpfreaks.com/topic/178865-solved-convert-difference-timestamp-to-hhmmss/#findComment-943654 Share on other sites More sharing options...
Glen Posted October 24, 2009 Author Share Posted October 24, 2009 That's the game, working perfectly. Thank You. -Glen Link to comment https://forums.phpfreaks.com/topic/178865-solved-convert-difference-timestamp-to-hhmmss/#findComment-943667 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.