Saturday, August 24, 2019

How to calculate the total number of days between two dates in PHP?

This articles is to find the difference between total number of days for two specified dates (e.g. start date and end dates).

 Examples : 

 Start date : 2019-02-07

 End date : 2019-05-12

 Here are two different function to find out the total number of days between two dates

 Example 1 : 

// Define start date and end date
 $start_date = date_create("2019-02-07");
 $end_date = date_create("2019-05-12");

// Get the difference between the total days of two dates
 $total_days = date_diff($start_date,$end_date);
 print "Total number of days between two dates: ".$total_days->format("%a");


Output :   Total number of days between two dates: 94

Example 2 : 

// Define start date and end date
 $start_date = strtotime("2019-02-07");
 $end_date = strtotime("2019-05-12");

// Get the difference between the total days of two dates
 $total_days = ($end_date - $start_date);
 print "Total number of days between two dates: " .  round($total_days / (60 * 60 * 24));

Output :   Total number of days between two dates: 94

Thats it !!!!

No comments:

Post a Comment

Popular Posts