Developer Snippet Diary

Function to get days between two dates php

The problem we want to solve is to calculate the number of days between two dates in PHP, regardless of the format of the date string.

function days_between($date1, $date2) {
  // Remove the day of the week from the date string
  $date1 = preg_replace('/\b\w+\b, /', '', $date1);
  $date2 = preg_replace('/\b\w+\b, /', '', $date2);

  // Create DateTime objects from the input dates
  $datetime1 = date_create($date1);
  $datetime2 = date_create($date2);

  // Calculate the difference between the two dates
  $interval = date_diff($datetime1, $datetime2);

  // Return the number of days in the interval
  return $interval->days;
}

Output:

echo days_between("Friday, 14 April 2023","Friday, 18 April 2023"); // Outputs: 4
echo days_between("2023-04-12 09:15:29", "2023-04-14 09:15:29"); // Outputs: 2
echo days_between("2023-04-12", "2023-04-15"); // Outputs: 3

 

The days_between() function takes two dates as input parameters and returns the number of days between them. To handle different date formats, the function first removes the day of the week from each date string using the preg_replace() function. It then creates DateTime objects from the input dates using the date_create() function, which can parse a wider range of date formats.

The function then calculates the difference between the two DateTime objects using the date_diff() function, which returns a DateInterval object. Finally, it extracts the number of days from the DateInterval object using the days property and returns it as the result of the function.

For example, if you call days_between("Friday, 14 April 2023","Friday, 18 April 2023"), the function will remove the day of the week from each date string, resulting in "14 April 2023" and "18 April 2023". It will then create DateTime objects from these dates, calculate the difference between them, and return the number of days as 4.

I hope this explanation helps you understand how the code works!

The days_between() function takes two dates as input parameters and returns the number of days between them. To handle different date formats, the function first removes the day of the week from each date string using the preg_replace() function. It then creates DateTime objects from the input dates using the date_create() function, which can parse a wider range of date formats.

The function then calculates the difference between the two DateTime objects using the date_diff() function, which returns a DateInterval object. Finally, it extracts the number of days from the DateInterval object using the days property and returns it as the result of the function.

For example, if you call days_between("Friday, 14 April 2023","Friday, 18 April 2023"), the function will remove the day of the week from each date string, resulting in "14 April 2023" and "18 April 2023". It will then create DateTime objects from these dates, calculate the difference between them, and return the number of days as 4.

I hope this explanation helps you understand how the code works!

How to get today date:

date("Y-m-d H:i:s")

Sample

echo days_between($job->created_at,date("Y-m-d H:i:s")) days Ago
Posted by: R GONDAL
Email: rizikmw@gmail.com