-1

I have to calculate the number of days that have passed between two dates, but I need to split the days difference per month and just display the current month passed days. This is needed to calculate vacations time for an employee.

Note: I need to make this on PHP 5.4. For example I'm going to use this two dates and a current month:

<?php
CurrentMonth="04";
FirstDate="2021-03-28";
SecondDate="2021-04-15";
?>

So, for this example the total day diff is 18 days, but I just need to return the 15 days passed in the current month "04".

Any suggestions? Thanks in advance.

  • How does `holidays time` relate to question? Is it just count of days for current month? – user3783243 Apr 14 '21 at 20:57
  • Yes, the first date is the beginning of holidays time and the second date refers to the end of holidays. – Rafael Granados Apr 14 '21 at 20:59
  • Not sure I follow, a holiday is normally 1 day. Like christmas is Dec 25, so some people dont want that in vacation time calculations. Is that what you mean or something else? – user3783243 Apr 14 '21 at 21:01
  • Sorry, I'm refering to vacations time – Rafael Granados Apr 14 '21 at 21:02
  • Does https://3v4l.org/XDZPS answer question? – user3783243 Apr 14 '21 at 21:05
  • Nope, the 1st date have to be in a different month and just need the days passed for the current month, for example, first date is "2021-03-28" and second date is "2021-04-02", so, for the first date have 3 days until the end of that month and the second date have 2 days from the beginning of that month, just need the days passed in the current month. – Rafael Granados Apr 14 '21 at 21:11
  • @user3783243 That seems sensible so why not post it as an answer – RiggsFolly Apr 14 '21 at 21:23
  • 1
    If you `just need the days passed in the current month` why does the first date/range matter? – user3783243 Apr 14 '21 at 21:24
  • @RiggsFolly Think the question is different, just not sure how. That also is just from the manual, not my code. – user3783243 Apr 14 '21 at 21:24

2 Answers2

1

Just made a little modification to Pablo's code for take the days diff if the current month is in the first date:

<?php
$currentMonth = '04';
$firstDate = new DateTime('2021-03-28');
$lastDay= new DateTime($firstDate->format( 'Y-m-t' )); //last day of $firstDate month
$secondDate = new DateTime('2021-04-15');
$firstDay= $secondDate->format( 'Y-m-01' ); //first day of $secondDate month
$firstDayDT = new DateTime($firstDay); //datetime for $firstDay

$firstDateMonth = $firstDate->format('m');
$secondDateMonth = $secondDate->format('m');

if ($currentMonth === $secondDateMonth) {
    $diff = $secondDate->format('j') - $firstDayDT->format('j')+1; //sum +1 day
} else if ($currentMonth === $firstDateMonth) {
    $diff = $lastDay->format('j') - $firstDate->format('j');
};
echo $diff;

If current month is "03", the code returns 3, if the month is "04", returns 15.

0

A simple solution for a simple problem:

$firstDate = new DateTime('2021-03-28');
$secondDate = new DateTime('2021-04-15');

$diff = $firstDate->format('n') < $secondDate->format('n')
    ? $secondDate->format('j')
    : $secondDate->format('j') - $firstDate->format('j');

Using the current month as variable:

$currentMonth = '04';
$firstDate = new DateTime('2021-03-28');
$secondDate = new DateTime('2021-04-15');

$firstDateMonth = $firstDate->format('m');
$secondDateMonth = $secondDate->format('m');

if ($currentMonth === $firstDateMonth && $currentMonth === $secondDateMonth) {
    $diff = $secondDate->format('j') - $firstDate->format('j');
} else {
    $diff = $currentMonth === $secondDateMonth
        ? $secondDate->format('j')
        : null;
}
Pablo LaVegui
  • 179
  • 1
  • 5