Reputation:
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?
Upvotes: 0
Views: 4924
Reputation: 11
I solved this problem as follows. It is not the best solution but at least it works:
$before_midnight = strtotime("23:59:59");
$before_midnight++; // this makes exact midnight
$start = strtotime("21:00");
$target = strtotime("03:00");
$after_midnight = strtotime("00:00");
for($i=$start; $i<$before_midnight; $i += 3600)
echo date("H:i", $i). "<br>";
for($i=$after_midnight; $i<=$target; $i += 3600)
echo date("H:i", $i). "<br>";
Upvotes: 1
Reputation:
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.
Upvotes: 0
Reputation: 1795
You have a string ('12:00'
) and are trying to compare it like a number.
https://www.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.
Upvotes: 0
Reputation: 81
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 ]]]]]]] )
Upvotes: 1
Reputation: 40675
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!
Upvotes: 5
Reputation: 158051
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 =)
Upvotes: 1