0

Please, i need to get the time difference between a particular time and the current time. ie. I want to track users who submit an assignment late. the deadline time is 10:00AM everyday.I have surf,but all the solution i am seeing does not seems to work. see below:

<?php

   $d1 = new DateTime(date('Y-m-d 10:00:00'));//Deadline time
   $d2 = new DateTime(date('Y-m-d H:i:s'));//Submission time
   $interval = $d1->diff($d2);
   $c = $interval->minute;
   if($c>0){
  echo "submitted late";}else{ echo "Submitted on time";}
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
David Mukoro
  • 467
  • 1
  • 8
  • 25

2 Answers2

0

Regarding the first point, your code doesn't work because $interval->minute is always positive. So you should simply compare two dates. Use the following code to achieve it:

    $d1 = new \DateTime(date('Y-m-d 10:00:00'));//Deadline time
    $d2 = new \DateTime(date('Y-m-d H:i:s'));//Submission time
    if($d1 < $d2) {
        echo "submitted late";
    } else { 
        echo "Submitted on time";
    }
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Andrej
  • 7,474
  • 1
  • 19
  • 21
0
$deadline = date('10:00:00');
$now      = date('H:i:s');

if($now > $deadline){
    echo "submitted late";
} else { 
    echo "Submitted on time";
}

as simple as that

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • this code seems to work, but I think, since i am comparing the deadline time with the current time, it shoud be (current time>deadline time) echo Submitted late else Submitted on time. because it is given the same output of submitted late especially when you compare 9:30 with 10:00. what coudl be the issue? – David Mukoro Aug 29 '16 at 12:59
  • this is why you should geve meaningful names to variables - not to confuse them. Now it's clear – Your Common Sense Aug 29 '16 at 13:16
  • @So, what is the Solution:, as the time is still not working...,becuase, changing the time to 1 minue to 10am, still shows late submission. – David Mukoro Aug 29 '16 at 13:19
  • @Did you try changing your time like 9:25 and see the response? A it is showing the submission late here? Finally, how can i prevent users from changing their clock to lower time before submission? – David Mukoro Aug 29 '16 at 13:25
  • Users' clock has nothing to do with your server. the latter edited code in my answer shows the right message for the any time given – Your Common Sense Aug 29 '16 at 13:28