John Davidson

datetime - What PHP is needed to calculate due date from todays date

0 comments
Message:


I need a few if/elseif/else statements (which I loosely understand) to display different things based on how far away a due date is from today. Example below:



  • If due date is > 4 weeks away, priority is low

  • If due date is < 4 weeks and > 2 weeks away, priority is medium

  • If due date is < 2 weeks and > 1 week away, priority is high

  • If due date is < 1 week away, priority is critical


Edited to add: the due date will vary across projects. I am trying to create a project management dashboard. So I am trying to compare $dt = new DateTime($Project[due_date]); to $today = new DateTime();


When I write out


if($dt < $today) {
echo 'urgent';
} else {
echo 'high';
}

I keep getting this error:



Catchable fatal error: Object of class DateTime could not be converted to string



Forgive me if this is a poorly written or newbie question, I am very new to PHP (HTML/CSS is my strength) so I am having a hard time knowing what to even Google to help me.



My Answer:
$dueDate = "2022-12-31";
$today = date("Y-m-d");

$diff = strtotime($dueDate) - strtotime($today);
$days = floor($diff / (60 * 60 * 24));

if ($days < 0) {
echo "Due date has passed";
} elseif ($days == 0) {
echo "Due date is today";
} elseif ($days == 1) {
echo "Due date is tomorrow";
} else {
echo "Due date is in $days days";
}

Rate this post

3 of 5 based on 8655 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us