Issues with showing a warning if the deadline is close on a task

Im doing a todolist and I want the deadline column to display a message if the deadline was coming close . But im having trouble writing the if condition for that.

[<?php
require_once ‘dbcon.php’;

      $current_date = date('Y-m-d'); 
     $datediffer = $fetch['deadline'] - $current_date;
    if($datediffer < 5){

      echo"<font color = yellow> Deadline coming up</font>";
    }     

its in the above part that there’s trouble with. The rest of the stuff below worked okay b4 i added the above.

      elseif($fetch['deadline'] < $current_date ){

        if($fetch['tid'] != ""){
          $tid = $fetch['tid'];
        echo "<font color=red>Missed deadline</font>";
        $conn->query("UPDATE `tasks` SET `p/f` = '1' WHERE `tid` = $tid") or die(mysqli_errno($conn));
        echo $fetch['deadline'];
        }
      }
       else{
        echo $fetch['deadline'];
       }
      
				?>](https://)

Th results i get on the page shows all of the tasks marked with the “Deadline coming up” including the ones which deadlines already passed. There’s also the following error " A non well formed numeric value encountered". If someone could please help me figure out what im doing wrong in this if condition I would greatly appreciate it.

What is in $fetch['deadline']? I mean, obviously it’s your deadline, but what format is it in?

As the php date() function returns a formatted date, in your case something like “2022-07-20”, what makes you think you can just subtract one string from another and get a number of days? That’s probably where your “non well formed numeric value” error comes, but the error message will have the code line number in it. Please edit your post to show which line this is. If you removed the “-” separators in the dates they would look more like numbers and would be fine for checking one is lower than another, but wouldn’t give you the number of days between two dates - think of the difference between 20220131 and 20220201 - it’s not ‘1’, as it would need to be.

To compare dates as you want to, you could use something like this: https://www.php.net/manual/en/datetime.diff.php

You also need to read up on “Prepared Statements” rather than concatenating values into your queries as you do here.

2 Likes

its querying the value from the database and thats saved as date type.

The error points to this line of code.

$datediffer = ($fetch[‘deadline’]) - $current_date;

That’ll be it then. You’re taking two strings, something like “2022-07-25” and “2022-07-19” and trying to subtract one from the other. You can’t do that because PHP just sees them as strings, not as dates - you’ll need to look at the link I posted.

I tried implementing what was in the link you sent. I dont think I didnt that well tho :sweat_smile:

   $current_date = new DateTime(date('Y-m-d')); 
      $deadline = new DateTime($fetch['deadline']);
      
      $datediffer = $current_date->diff($deadline);
      $datediffer->format('%R%a days');
      
      echo $datediffer;
    if ($datediffer > 5) {

      echo "<font color=yellow>warning deadline close</font>";
      echo $fetch['deadline'];
    }  

but I get another error that states Object of class DateInterval could not be converted to string. It points to

echo $datediffer;
Any ideas?

Because $datadiffer is an object, you can’t just echo it. There are examples in the documentation I linked to of how to display the result. There’s also a link to the documentation for the format() method which would allow you to return just the number of days, which I believe is what you’re looking for.

Consider this code:

$current_date = new DateTime(date('Y-m-d')); 
$deadline = new DateTime($fetch['deadline']);
      
$datediffer = $current_date->diff($deadline);
$days = $datediffer->format('%R%a');
      
echo $days;
if ($days > 5) {
  echo "<font color=yellow>warning deadline close</font>";
  echo $fetch['deadline'];
  }  

I suspect you want to be checking when the difference between the two dates is smaller than 5, not greater, though.

1 Like

yeeps just realised that so i changed it to
if ($days < 5 && $days > 0 )

That section of the code works now but after that change, the deadline passed section isnt working the same. Meaning the deadlines that are not close are being marked as passed the deadline. Id done it like this initially.

elseif($fetch[‘deadline’] < $current_date ){

    if($fetch['tid'] != ""){
      $tid = $fetch['tid'];
    echo "<font color=red>Missed deadline</font>";
    $conn->query("UPDATE `tasks` SET `p/f` = '1' WHERE `tid` = $tid") or die(mysqli_errno($conn));
    echo $fetch['deadline'];
    }
  }
   else{
    echo $fetch['deadline'];
   }

I tried changing it thinking the current time variable was messing up again. So i put this statement

$act_current = $current_date->format(‘%R%a’);
directly under the $days = $datediffer->format(‘%R%a’); before the entire if statements start and called it like this

$current_date = new DateTime(date(‘Y-m-d’));
$deadline = new DateTime($fetch[‘deadline’]);

        $datediffer = $current_date->diff($deadline);
        $days = $datediffer->format('%R%a');
        $act_current =  $current_date->format('%R%a');      
        
        if ($days < 5 && $days > 0 ) {
          echo "<font color=yellow>warning deadline close</font>";
          echo $fetch['deadline'];
          }  

//deadline missed condition
elseif($fetch[‘deadline’] < $act_current ){

        if($fetch['tid'] != ""){
          $tid = $fetch['tid'];
        echo "<font color=red>Missed deadline</font>";
        $conn->query("UPDATE `tasks` SET `p/f` = '1' WHERE `tid` = $tid") or die(mysqli_errno($conn));
        echo $fetch['deadline'];
        echo $act_current;
        }
      }
      
       else{
        echo $fetch['deadline'];
       }

but now the ones with the deadline passed isnt working at all.

Can you edit your post to have the code markers in the right place please? It’s unreadable like that.

But…

elseif($fetch[‘deadline’] < $act_current ){

you’re back to comparing strings now. You’re comparing something like “2022-08-02” with something different. What’s actually in $act_current when you echo it out? I take it your actual code uses proper quotes around “deadline”, too?

Surely for “deadline passed” all you need to check is for $days < 0 ?

1 Like

Im really sorry about that. I used ctrl k to get that to go into the right format but for some reason it always leaves out the first line. Ill keep trying to edit so its easier.

Nothing gets echoed at all. That code block must not be getting executed at all.

yeps it does.

draws deep breath, evidently my stupidity knows no bounds. I apologise sincerely for the trouble. Thank you VERY much for your help.

No, it’s being executed. What’s going wrong here is that you’re using the format string that you used for $datediffer, but that’s a DateInterval object. $current_date is a DateTime object, which uses different format strings: https://www.php.net/manual/en/datetime.format.php

Strange that nothing gets echoed - when I try it, I get %R%am rather than nothing. But you can see that’s not a valid number to be comparing to anything.

I’m not sure what ctrl-k would do, I just put a line that contains only three back-tick characters at the start and end of code. I’m not sure how to show that without going into a code block.

ohhh okay, looks like i have a lot to go read over then. Thank you for the reference material.

before I did all the jazz with the addition of $act_current id tried just echoing the current date and id got an error that had to do with string type which was why i then created the other variable to convert and see if it would work. But nothing echoed then. weird that you get %R%am. I must have missed something out when I was echoing it then.

other sites I used would put it into code format when i used it. My mistake. Il be sure to use back-ticks in the future.

Thank again for your help, tc.