Jump to content

[SOLVED] Convert difference timestamp to hh:mm:ss


Glen

Recommended Posts

Hi everyone,

 

PHP times and date are not my strongest part of php  :P

 

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 :D

 

Many Thanks

 

-Glen

 

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

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.