tags:

views:

580

answers:

5

I have an array of times I want to print out. I want the times that have passed lets say 12:00 clock to be 'greyed out'.

$theTime = '12:00';

if($theTime >= $time[$i]) {....}

02:30
03:50
03:20
04:50
05:45
19:45
20:00
20:50
20:55
21:25
21:30
22:00
22:45
23:55
00:50
00:55

Im doing a simple compare 12:00 a clock to each value. The problem occurs when you change the time to after midnight for example 00:15. How can I calculate and print the list in order, when time has passed midnight?

+1  A: 

Use unix timestamps. create a unix time stamp for midnight, and then compare all of the others to that. Then format it as a time when you print it out.

(A while since I used PHP, so can't remember quite how to do it, but should be simple. I know I have done something similar before. Take a look at http://php.net/time, http://php.net/manual/en/function.mktime.php and http://php.net/manual/en/function.date.php. Should be simple enough =)

Svish
+5  A: 

if you pass midnight, more than one day is involved. this means that you have to include the information of day! what day is it? so in order to achieve what you want you should list/store more than just the time! if you store a datetime value, you will have no problems calculating time differences, since php will know in what order to put the times according to the day information.

for that look at the php datetime functions.

they will also help you to calculate differences!

tharkun
A: 

Re. Svish's suggestion - strtotime() is handy for easily creating Unix timestamps relative to the current time, or any arbitrary time.

e.g. strtotime('midnight') will give you the unix timestamp for the most recent midnight.

Beyond that - I can't make head or tail of your question :)

A: 

As Svish said, you should use real timestamps, and you should also check the date change... here the, I think, more quick and easy way to know difference between 2 time (and date) :

<?php
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes.";
?>

note the $date1 and $date2 have to be in mktime format, as :

int mktime  ([ int $hour=date("H")  [, int $minute=date("i")  [, int $second=date("s")  [, int $month=date("n")  [, int $day=date("j")  [, int $year=date("Y")  [, int $is_dst=-1  ]]]]]]] )
RVeur23
A: 

You have a string ('12:00') and are trying to compare it like a number.

http://us3.php.net/manual/en/language.operators.comparison.php

Like Svish and Paul said, you need to use integer timestamps.

$now = time(); // Get the current timestamp

$timestamps= array(strtotime('midnight'),
                   strtotime('07:45'), 
                    ...
                   );

foreach ( $timestampsas $time ) {
    if ( $time >= $now ) {
        // $time is now or in the future
    } else {
        // $time is in the past
    }
}

You can format the timestamps with the date function.

James Socol