skip to Main Content
`$flyingdate = date('Y-m-d H:i:s');
$task_end_date = '2023-05-15'; 
$delays = ($flyingdate - $task_end_date);`

Problem is that, String variables can not no be changed,
I tried with date difference and string comparison function but useless
It can not tell the exact difference of days i.e. actually telling delays of days from flying to task end date,
I want difference in term of days(store in $delays) and also condition should be
delays set to zero , if days are negative;

2

Answers


  1. You can do like this, time() returns the current timestamp, strtotime also returns timestamp, so substracting both and converting the seconds to days

    $flyingdate = time();
    $task_end_date = strtotime('2023-05-15');
    $delays = $flyingdate - $task_end_date;
    
    $days_diff = $delays / (60 * 60 * 24);
    $rounded_days_diff = (int)floor($days_diff);
    
    Login or Signup to reply.
  2. Hi Hafiz i hope you are doing well, as i can see that when you declare $task_end_date variable you declared it as a string and $flyingdate you use date class what i recommend is to use date class in both variables, so you can calculate the difference in days between two dates there is the code i recommend you to use :

    $flyingdate = new DateTime();
    $task_end_date = new DateTime('2023-05-15');
    
    // To calculate the difference between the dates
    $interval = $flyingdate->diff($task_end_date);
    
    // Change the difference to days format
    $delays = $interval->format('%r%a');
    
    // Check if the difference is negative
    if ($delays < 0) {
        $delays = 0;
    }
    
    // Display the delays
    echo "Delays: $delays days";
    

    As you can see in this code,we have DateTime objects for the flying date and the task end date. Then, we calculate the difference between the two dates using the diff() method. We use the %r%a format to get the signed difference in days, then we use a condition that check if the date difference in negative.
    have a good day sir .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search