Dark King Rayleigh Dark King Rayleigh - 2 months ago 11
PHP Question

Comparing time in PHP

I am comparing a deadline time from my database which is on a 24-hour format (like 3:00 = 15:00) to the time now.

For example, if a student wasn't able to submit on the due time, an upload link will be greyed out.

I'm okay with the date comparison, my problem is the time.

Here's my current code:

$date = date("Y-m-d");
$time = strtotime(date('G:i:s'));
$deadtime = strtotime($r['upload_deadtime']);

/*------$deadtime = 15:00:00 ---------*/

if(date($r['upload_deadline']) >= $date && $deadtime > $time){

echo '<td><a href="stud_prep.php">upload</a></td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}


Update: let's just forget about the date comparison, what I'm trying to say is how can I compare my deadline time (which is on a 24-hour format (like 3:00:00 = 15:00:00)) to the time now.

Here's my current code:

$time = strtotime(date('G:i:s')); /*which returns 23:15:42 instead of 14:15:42 */
$deadtime = strtotime('15:00:00');


if($deadtime > $time){

echo '<td><a href="stud_prep.php">upload</a></td>';
}
else{
echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';
}

Answer
date_default_timezone_set('Asia/Manila');

$deadtime = strtotime("15:00:00");

if ($deadtime - time() > 0){
  echo '<td><a href="stud_prep.php">upload</a></td>';                 
}else{
  echo '<td><a title="your too late! haha!" style="color:grey">upload</a></td>';  
}

This may solve your time comparison issue!!

strtotime("15:00:00") will convert time into unix timestamp of today's 15:00:00 and time() have current unix timestamp.

Comments