3

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?

14
  • Just add one to the output of your calculations
    – Andreas
    Jun 9, 2018 at 18:16
  • @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. Jun 9, 2018 at 18:29
  • 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
    Jun 9, 2018 at 18:38
  • @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. Jun 9, 2018 at 18:42
  • 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? Jun 9, 2018 at 18:43

6 Answers 6

5

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

3
  • Can you explain me more? Jun 9, 2018 at 18:20
  • DateTime::diff -- DateTimeImmutable::diff -- DateTimeInterface::diff -- date_diff — Returns the difference between two DateTime objects
    – Rashed
    Jun 9, 2018 at 18:23
  • 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");
    – Perelx
    May 28, 2019 at 20:31
1

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 
)
2
  • I need an answer 46, not 45, Because in the January total days is 31 and in fab 15 so 31+15=46 Jun 9, 2018 at 18:23
  • You can modify the first date, if you need to include the start date as count: $date1->modify('-1 day');
    – Tyr
    Jun 9, 2018 at 20:35
1
<?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.

2
  • 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 Jun 9, 2018 at 18:35
  • The function date_diff(datetime1,datetime2,absolute); returns a DateInterval object on success that represents the difference between the two dates. FALSE on failure
    – skm
    Jun 9, 2018 at 18:52
1

You can try this:

  <?php
    $start_date = "2020-12-24";
    $end_date   = "2020-12-31";
    $dateDiff   = strtotime($end_date) - strtotime($start_date);
    $numOfDays  = $dateDiff / 86400;
    echo $numOfDays;
  ?>

Output: 7

0

First compile and print $now and write down out put after some minutes 2nd time compile and print $now you will ge a difference

0

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

3
  • 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. Jun 9, 2018 at 18:32
  • I have to get the value from the user not from the time() function. Time() will dislay the current date. Jun 9, 2018 at 18:34
  • Not only day it is int form of date and time
    – manan5439
    Jun 9, 2018 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.