## How to count days between two dates in PHP?

If I have a couple of strings `$startDate`

and `$endDate`

which are set to (for instance) `"2011/07/01"`

and `"2011/07/17"`

(meaning 1 July 2011 and 17 July 2011). How would I count the days from start date to end date? In the example given, it would be 17 days.

Here is the raw way to do it

$startTimeStamp = strtotime("2011/07/01"); $endTimeStamp = strtotime("2011/07/17"); $timeDiff = abs($endTimeStamp - $startTimeStamp); $numberDays = $timeDiff/86400; // 86400 seconds in one day // and you might want to convert to integer $numberDays = intval($numberDays);

**Program to find the number of days between two dates in PHP ,** Method 2: To use date-time mathematical formula to find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between i created a function in which if you pass two dates than it will return day wise value. For better understanding please see the output of start date : 2018-11-12 11:41:19 and End Date 2018-11-16 12:07:26

Use `DateTime::diff`

(aka `date_diff`

):

$datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2);

Or:

$datetime1 = date_create('2009-10-11'); $datetime2 = date_create('2009-10-13'); $interval = date_diff($datetime1, $datetime2);

You can then get the interval as a integer by calling `$interval->days`

.

**How to calculate the difference between two dates in PHP ,** If you want to find out the number of days between two dates, use below. You can change to a different unit from that. It looks a little insane, but keep in mind the Calculate the number of days between two dates in PHP Method 1# First we will convert start date and end date into to unix timestamps using strtotime( ) function, then substract end date to start date then it will give you the difference in seconds, which you divide by 86400 (total seconds in a day) to give you an approximate total of days in that range.

PHP has a date_diff() function to do this.

**date_diff - Manual,** PHP count days between two dates - Calculate the difference between two dates in PHP. Use date_create() and date_diff() function to get Method 2 Using date_diff () Function: The date_diff () function is an inbuilt function in PHP which is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.

<?php $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%a days'); ?>

Source: http://www.php.net/manual/en/datetime.diff.php

**PHP date_diff() Function,** The Duration Calculator calculates the number of days, months and years between two dates. Method 1: Use date_diff() Function to find the difference between two dates. Method 2: To use date-time mathematical formula to find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between two specified dates.

In case your DateTime has also hour:minutes:seconds and you still want to have the number of days..

/** * Returns the total number of days between to DateTimes, * if it is within the same year * @param $start * @param $end */ public function dateTimesToDays($start,$end){ return intval($end->format('z')) - intval($start->format('z')) + 1; }

https://github.com/dukeatcoding/timespan-converter

**How to Get Number of Days Between Two Dates in PHP,** Source Code and Output to get number of days between two dates. <?php $date1="2016-07-31"; $date2="2016-08-05"; function dateDiff($date1, $date2) How to get number of days between two dates in php? Answers. Mourya. April 21, 2020 . We can get the days between two dates by below code.

**Date Duration Calculator: Days Between Dates,** Write a PHP script to calculate number of days between two dates. Sample Solution: PHP Code: <?php $to_date = time(); // Input your date here Divide the difference in seconds between our two dates by 86400 and you’ve got the number of days that have passed. You can then use the floor function in order to round the result down. i.e. 2.9 days = 2 days. To get the number of minutes, you can simply divide the difference in seconds by 60, like so: As you can see, it’s pretty simple.

**PHP code to get number of days between two dates,** First we will convert start date and end date into to unix timestamps using strtotime() function, then substract end date to start date then it will give The Duration Calculator calculates the number of days, months and years between two dates.

**PHP Date Exercise: Calculate number of days between two dates ,** Definition and Usage The date_diff () function returns the difference between two DateTime objects.