-1
`$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;

3 Answers3

1

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 .

0

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);
Paulo Fernando
  • 3,148
  • 3
  • 5
  • 21
-1

It worked, thanks but now, the problem is that I am working in PHP with a database on localhost, each time my $flyingdate and $delays are updated, I want it to update only one time. I want this if condition to run once when $status == 100 and some condition that show do not enter in loop, I tried with empty $flyingdate, but it become empty every time I run the code, I want some condition that it will always work one time, as in below code,

  if(($status == 100) && (empty($flyingdate))){
        $flyingdate = date('Y-m-d H:i:s');
        $flyingtime = time();
        $task_end_date = strtotime($last_end_date);
        $delays1 = $flyingtime - $task_end_date;
        $days_diff = $delays1 / (60 * 60 * 24);
        $delays = (int)floor($days_diff);
    }
shaedrich
  • 5,457
  • 3
  • 26
  • 42
  • I didn't understand what you need, when the $flyingdate and $delays becomes updated? It becomes empty every time you run the code, that's the expected behaviour not? when you run it starts fresh – Paulo Fernando Jun 21 '23 at 23:11
  • But I have to change this if condition in such a logic, that it should run one time only, any function in PHP that run one time for each $ aircraft_ID? There are Multiple aircrafts, I want that this loop will apply for each aircraft_ID one time only as many we refresh the code or webpage.. flydate in this way always changes according to recent day and time, I want it restricted to the date and time, when it first time takes in this loop throughout life. – Hafiz M Osama Jun 22 '23 at 04:28
  • You are going tô update the flying date in the database? You can Just query to ser If It was already updated – Paulo Fernando Jun 22 '23 at 11:29