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?

user9437856
  • 2,360
  • 2
  • 33
  • 92
  • Just add one to the output of your calculations – Andreas Jun 09 '18 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. – user9437856 Jun 09 '18 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 09 '18 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. – user9437856 Jun 09 '18 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? – user9437856 Jun 09 '18 at 18:43
  • It will be good for me if share the reason for the downvote. I will improve in the future. – user9437856 Jun 09 '18 at 18:51
  • You are not listening. Quite frankly, if you are building code for salary calculations then just give it up. – Andreas Jun 09 '18 at 18:52
  • @Andreas, Thanks for the suggestion. If you want to display the total working days in the last month then how can you display it? – user9437856 Jun 09 '18 at 18:56
  • By adding one (+1) to the calculation! Here I will spell it out loud and clear now: https://3v4l.org/9QeoT – Andreas Jun 09 '18 at 18:59
  • @Andreas, Cool!!. Is it a good thing to do? I mean adding a (+1). just for my information. – user9437856 Jun 09 '18 at 19:02
  • Oh! you added the +1 information in the second comment. I forgot to check that. Thanks @Andreas – user9437856 Jun 09 '18 at 19:04
  • Finally!! Yes! If you always calculate something to be one day less than you want it to be, then just add one to the calculation. And it's a perfectly fine way of doing it. As long as you are not expecting 30 one time and 31 the next. – Andreas Jun 09 '18 at 19:04
  • I wrote *Just add one* 48 minutes ago on my first comment too – Andreas Jun 09 '18 at 19:06
  • @Andreas, yes, I forgot to check that because of lots of notification was coming. Thanks for the help, I will try this. – user9437856 Jun 09 '18 at 19:07

6 Answers6

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

Rashed
  • 2,349
  • 11
  • 26
  • Can you explain me more? – user9437856 Jun 09 '18 at 18:20
  • DateTime::diff -- DateTimeImmutable::diff -- DateTimeInterface::diff -- date_diff — Returns the difference between two DateTime objects – Rashed Jun 09 '18 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"); – user8735467239 May 28 '19 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 
)
Tyr
  • 2,810
  • 1
  • 12
  • 21
  • I need an answer 46, not 45, Because in the January total days is 31 and in fab 15 so 31+15=46 – user9437856 Jun 09 '18 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 09 '18 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.

skm
  • 1,650
  • 2
  • 10
  • 17
  • 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 – user9437856 Jun 09 '18 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 09 '18 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

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

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

ice1000
  • 6,406
  • 4
  • 39
  • 85
manan5439
  • 898
  • 9
  • 24