开发者

PHP select current time in cell

If I use PHP code like:

for ($i = 1; $i <= 24; $i++)
 {
 $timetable=strftime("%X", mktime(0, 0, $hour[$i], $month, $day, $year));
 }

to get timetable for current day that looks like:

   time   |
 ----------
 05:09:23 |
 ----------
 07:23:18 |
 ----------
 11:55:41 |
 ----------
 14:01:32 |
 ----------

I want to hi开发者_运维百科ghlight cell where the printed time include the current time (if the current time is 9:23:00 highlight cell will be the one with the printed time 07:23:18 and if the current time is 13:59:06 highlight cell will be 11:55:41)

How can I do that?


Use this to do it:

$thisID = 0;
for ($i = 1; $i <= 24; $i++)
{
    $tt = mktime(0, 0, $hour[$i], $month, $day, $year);
    $timetable = strftime("%X", $tt);

    if($thisID == 0 && (time() - $tt) >= 0)
         $thisID = $i;
}

Now $thisID gives you the required row. Use it to highlight the row.


There's a date_diff() function you can use to determine which one is closest. link date_diff Or, if this info is coming from the database, you can use your database's date/time handling functions to return the time difference between Now() and the date being pulled back as a column.


The best way to do this In my opinion is to store the times in unix timestamps or seconds from midnight. This way you have a solid way of sorting your data.

To get the closest record you will need to do 2 queries.

Select time from times where time > $value order by time desc limit 1
Select time from times where time < $value order by time asc limit 1

Now compare these 2 results to $value and return the closest one.

Although this method requires 2 queries and uses a greater/less than statement, I think ist is faster than retrieving all data.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜