Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caculation of DATEDIF result is partially wrong #1465

Closed
albanduval opened this issue May 11, 2020 · 0 comments
Closed

Caculation of DATEDIF result is partially wrong #1465

albanduval opened this issue May 11, 2020 · 0 comments

Comments

@albanduval
Copy link
Contributor

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

DATEDIF calculated result should return the same result as MS Excel

Diff in years ("Y" unit) :

  • 2000-02-02 => 2001-02-01 : should return 0
  • 2001-05-04 => 2020-05-03 : should return 18

Diff in months with years ignored ("YM' unit) :

  • 2000-02-02 => 2001-02-01 : should return 11
  • 2001-05-04 => 2020-05-03 : should return 11

What is the current behavior?

Diff in years ("Y" unit) :

  • 2000-02-02 => 2001-02-01 : returns -1
  • 2001-05-04 => 2020-05-03 : return 17

Diff in months with years ignored ("YM' unit) :

  • 2000-02-02 => 2001-02-01 : return -1
  • 2001-05-04 => 2020-05-03 : return -1

What are the steps to reproduce?

<?php

require_once __DIR__ . '/vendor/autoload.php';

$intervals = [
    [ 'start' => '2000-02-02', 'end' => '2001-02-01' ],
    [ 'start' => '2001-05-04', 'end' => '2020-05-03' ],
    ];

foreach($intervals as $interval) {
    echo "{$interval['start']} => {$interval['end']}\n";
    $startDate = new \DateTime($interval['start']);
    $endDate = new \DateTime($interval['end']);

    $diffY = \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEDIF($startDate, $endDate, 'Y');
    echo "  >  $diffY years\n";

    $diffYM = \PhpOffice\PhpSpreadsheet\Calculation\DateTime::DATEDIF($startDate, $endDate, 'YM');
    echo "  >  $diffYM months\n";
}

Which versions of PhpSpreadsheet and PHP are affected?

master branch since at least 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant