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

 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.