Jump to content

Table Timestamp Help


hellouthere

Recommended Posts

I have a table with one column showing a time (duration) so not 24hours etc... it can be anything up to 9999:59:59... Another column in the table displays a rank slide based on the "time". Here is the code...
[code]
print "<tr>";
    print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$num</font></td>";
    print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$name</font></td>";
if ($time < '10') {
print '<td width=50 height=12 align=left><img alt="Flight Engineer" src="images/FE.gif"</img></td>';
    }else if ($time > '750') {
print '<td width=50 height=12 align=left><img alt="Chief Pilot" src="images/CP.gif"</img></td>';
}else if ($time > '500') {
print '<td width=50 height=12 align=left><img alt="Fleet Captain" src="images/FCapt.gif"</img></td>';
}else if ($time > '250') {
print '<td width=50 height=12 align=left><img alt="Captain" src="images/Capt.gif"</img></td>';
}else if ($time > '100') {
print '<td width=50 height=12 align=left><img alt="Co-Pilot" src="images/CoP.gif"</img></td>';
}else if ($time > '10') {
print '<td width=50 height=12 align=left><img alt="Flight Officer" src="images/FO.gif"</img></td>';
}
print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$time</font></td>";
print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$earn</font></td>";
if ($time > '0') {
    print '<td width=71 height=12 align=left><img alt="Active" src="images/act.png"</td>';
}else {
print '<td width=71 height=12 align=left><img alt="Inactive" src="images/inact.png"</td>';
}
    print "</tr>";
[/code]

At the moment if someone has over 10 hours it displays the rank slide for >100 i think i know why it does this... it is interpreting 10:08:00 (10 hours 8 minutes) as 100:8:00 (it just keeps counting zeros until it hits anotehr number... ive tried other times and the results support my theory... ive tried using the format 10:00:00 in the if statements but no luck... it also wont accept decimals... any help is apreciated... Thanks in advance...
Link to comment
https://forums.phpfreaks.com/topic/27668-table-timestamp-help/
Share on other sites

You can use this function to convert your time into hours

[code]<?php
function timeToHours ($time) {

  //Split time into an array
  $timeAry = explode (":",$time);

  //Convert into a decimal equiv for hours
  //Ex: 1:30:00 = 1.5
  $hours = 0;
  foreach($timeAry as $exp => $value) {
    $exp += (3-count($timeAry));
    $hours += $value / pow(60,$exp);
  }
  return $hours;
}

$time = "30:55";
echo timeToHours($time); //0.515277777778

$time = "1:30:55";
echo timeToHours($time); //1.515277777778

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/27668-table-timestamp-help/#findComment-126684
Share on other sites

the function would have to go in a piece of code like this;

[code]
for ($i=0; $i<$number; $i++) {
[/code]

The variable $time is set inside this code aswell and the function does not like being called twice... is it easy to split it into 2 parts or something similar?
Link to comment
https://forums.phpfreaks.com/topic/27668-table-timestamp-help/#findComment-127035
Share on other sites

Replace your original if statemetns to use $hours instead of $time (I say to do this in case you need the original $time variable for display or other puposes). Then just before your if statements use the function I gave you liske this $hours = timeToHours ($time);

[code]<?php
print "<tr>";
print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$num</font></td>";
print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$name</font></td>";

$hours = timeToHours ($time);
if ($hours < '10') {
    print '<td width=50 height=12 align=left><img alt="Flight Engineer" src="images/FE.gif"</img></td>';
}else if ($hours > '750') {
    print '<td width=50 height=12 align=left><img alt="Chief Pilot" src="images/CP.gif"</img></td>';
}else if ($hours > '500') {
    print '<td width=50 height=12 align=left><img alt="Fleet Captain" src="images/FCapt.gif"</img></td>';
}else if ($hours > '250') {
    print '<td width=50 height=12 align=left><img alt="Captain" src="images/Capt.gif"</img></td>';
}else if ($hours > '100') {
    print '<td width=50 height=12 align=left><img alt="Co-Pilot" src="images/CoP.gif"</img></td>';
}else if ($hours > '10') {
    print '<td width=50 height=12 align=left><img alt="Flight Officer" src="images/FO.gif"</img></td>';
}

print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$time</font></td>";
print "<td width=71 height=12 align=left><font face=Arial size=1 color=#313398>$earn</font></td>";

if ($hours > '0') {
    print '<td width=71 height=12 align=left><img alt="Active" src="images/act.png"</td>';
}else {
    print '<td width=71 height=12 align=left><img alt="Inactive" src="images/inact.png"</td>';
}

print "</tr>";
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/27668-table-timestamp-help/#findComment-128076
Share on other sites

Thanks mjdamato!
    Ive managed to define the dunction and make it work in the for($i=0... etc... but the function returns $hours as 0 everytime... this is the function at the moment:

[code]
function timeToHours()
{
//Split time into an array
$timeAry = explode (":",$time);

//Convert into a decimal equiv for hours
//Ex: 1:30:00 = 1.5
$hours = 0;
foreach($timeAry as $exp => $value) {
$exp += (3-count($timeAry));
$hours += $value / pow(60,$exp);
}
  return $hours;
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/27668-table-timestamp-help/#findComment-128406
Share on other sites

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.