## How to display total number of days between two dates using php

php difference between two dates in years, months and days

difference between two dates in php

php time difference in seconds

php time difference in minutes

php date

date between in php

calculate days between two dates

I want to count the total number of days between two date.

Note: I am not talking about the difference between two date

I found lots of answer on google and Stack Overflow. I am sharing here

$now = time(); // or your date as well $your_date = strtotime("2018-06-01"); $datediff = $now - $your_date; echo round($datediff / (60 * 60 * 24));

Output is 9

$now = strtotime("2018-06-09"); $your_date = strtotime("2018-06-01"); $datediff = $now - $your_date; echo $numberDays= round($datediff / (60 * 60 * 24));

Output is 8

$dStart = new DateTime('2018-06-01'); $dEnd = new DateTime('2018-06-09'); $dDiff = $dStart->diff($dEnd); echo $dDiff->days;

Output is 8

$date1=date_create("2018-06-01"); $date2=date_create("2018-06-09"); $diff=date_diff($date1,$date2); echo $diff->format("%a");

Output is 8

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

Output is 8

The correct answer for me is the first one 9 because it displays the total number of days from 1 to 9, not a difference.

I just want to know how to get the count because I don't want to use the time() because it's taking the current date.

For example: If I select the first date 2018-05-01 and my second date in 2018-05-31. So total days is 31, not 30.

Example: I am working on a project which is related to the working days and salary. So employee will select the date. for example. I am the employee and in the last month May, I select the date 2018-05-01 and 2018-05-31 so I have to display the total working days.So the according to the date I worked 31 days and using above code in the question I am getting 30 days.

Hope you understand my issue. Would you help me out in this?

Use the `DateTime`

class and compare them via `diff()`

method. `$date1->diff($date2)`

. The output will give you an accurate result of days:

$date1 = new DateTime('2018-01-01'); $date2 = new DateTime('2018-02-15'); print_r($date1->diff($date2));

Output:

DateInterval Object ( [y] => 0 [m] => 1 [d] => 14 [h] => 0 [i] => 0 [s] => 0 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 45 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )

**Finding the number of days between two dates,** How to find number of days between two dates using PHP? share. Using ceil rounds the amount of days up to the next full day. Use floor instead if you want to get the amount of full days between those two dates. If your dates are already in unix timestamp format, you can skip the converting and just do the $days_between part.

You can try this-

$dateOne = new DateTime("01-01-2018"); $dateTwo = new DateTime("09-06-2018"); echo $diff = $dateTwo->diff($dateOne)->format("%a");

**Output-** 159

**How to Get Number of Days Between Two Dates in PHP,** PHP count days between two dates - Calculate the difference between two dates in PHP. Use date_create() and date_diff() function to get Display the total number of days between two dates in PHP Resize an image with the same aspect ratio or with custom and crop sizes in PHP Bootstrap 4 – How to get button groups that span the full width of a parent

<?php $date1=date_create("2013-03-15"); $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2); echo $diff->format("%R%a days"); ?>

Output : 272 days.

**PHP code to get number of days between two dates,** In this example we will learn how to get difference between two times, here is the php code to get difference between two times. Two PHP functions are used in the above script to get the number of days between two dates. date_create() – This function returns a new DateTime object. date_diff() – This function returns the difference between two DateTime objects.

First compile and print `$now`

and write down out put after some minutes 2nd time compile and print `$now`

you will ge a difference

**date_diff - Manual,** $total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60; break; case "h": If you want to find out the number of days between two dates, use below. You can change to myClass->Sec; // Display only month for all duration The task is to find the difference between two dates. Examples: 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.

here i got your solution in first program php time(); function is dynamically change as per current time in millisecond (In PHP you can simply call time() to get the time passed since January 1 1970 00:00:00 GMT in seconds) here you are using round function to get rounded value after 12pm it your calculation will increase more then 8.5 and you get a rounded value which is 9

here brief explanation Getting unix timestamp in milliseconds in PHP5 and Actionscript3

in rest of program you are taking static time or custom time which are not effected by system time

$now = time(); // or your date as well $your_date = strtotime("2018-06-01"); $datediff = $now - $your_date; echo round($datediff / (60 * 60 * 24));

try above program before 12pm maybe you got your answer

**DateTime::diff - Manual,** The DateInterval object representing the difference between the two dates or FALSE on failure. See Also ¶ here's a simple alternative to get the number of days between two dates in the format '2010-3-23' February has 28/29 days total. If you multiply the figures above, it will give you 86400, which is total the number of seconds that is in a given day. 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.

**PHP Date Exercise: Calculate number of days between two dates ,** PHP date: Exercise-7 with Solution. Write a PHP script to calculate number of days between two dates. Sample Solution: PHP Code: <?php I do not have much programming in php and I hope I can help those that I want to do is that when entering in the form the date 1 and the date2 I calculate if between those two dates if they have passed 5 or more years and I add 3 more days taking As reference date 2, I do not know if I understand.

**PHP date_diff() Function,** Return Value: Returns a DateInterval object on success that represents the difference between the two dates. FALSE on failure. PHP Version Find number of days between two given dates Given two dates, find total number of days between them. The count of days must be calculated in O(1) time and O(1) auxiliary space.

**MySQL DATEDIFF() Function,** Parameter, Description. date1, date2, Required. Two dates to calculate the number of days between. (date1 - date2). Technical Details. Works in: From MySQL Date to Date Calc. Business Date to Date Date Calc. Business Date Dialing Codes Distance Travel Time Weekday Week Number Home Calculators Date Duration Calculator Days Calculator: Days Between Two Dates

##### Comments

- Just add one to the output of your calculations
- @Andreas, In the January month, total days is 31. So You start the work from 2018-01-01 to 2018-01-31 so if I check the different, It will display 30 but I worked 31 days in the company. So I need output 31. Hope you understand this.
- Yes, and if you get 30 from the calculation you just add 1 to it and you get 31. Why make it any harder than it needs to be?
- @Andreas, I am working on a project which is related to the working days and salary. So employee will select the date. for example. I am the employee and in the last month May, I select the date 2018-05-01 and 2018-05-31 so I have to display the total working days.
- So the according to the date I worked 31 days and using above code in the question I am getting 30 days. Any more idea in this?
- I need an answer 46, not 45, Because in the January total days is 31 and in fab 15 so 31+15=46
- You can modify the first date, if you need to include the start date as count:
`$date1->modify('-1 day');`

- Can you explain me more?
- DateTime::diff -- DateTimeImmutable::diff -- DateTimeInterface::diff -- date_diff — Returns the difference between two DateTime objects
- FYI, this will technically output negative days, if you add %r for the sign. If this were a "start" and "end" situation, you'd want to flip it to be: $dateOne->diff($dateTwo)->format("%a");
- It will be good for me if you use my date and time which I added to the question for easy, quick and fast to understand
- The function date_diff(datetime1,datetime2,absolute); returns a DateInterval object on success that represents the difference between the two dates. FALSE on failure
- In the January month, total days is 31. So You start the work from 2018-01-01 to 2018-01-31 so if I check the different, It will display 30 but I worked 31 days in the company. So I need output 31.
- I have to get the value from the user not from the time() function. Time() will dislay the current date.
- Not only day it is int form of date and time