Skip to content

Instantly share code, notes, and snippets.

@madgrk
Created August 1, 2019 13:54
Show Gist options
  • Save madgrk/70b0de0d797337890dd702fccc1338b5 to your computer and use it in GitHub Desktop.
Save madgrk/70b0de0d797337890dd702fccc1338b5 to your computer and use it in GitHub Desktop.
Show Difference In Days Between Two Dates
<?php
class DifferenceInDays {
private $difference;
protected $start;
protected $end;
function __construct($startDateTime, $endDateTime) {
if (is_string($startDateTime))
$this->start = date_create($startDateTime);
if (is_string($endDateTime))
$this->end = date_create($endDateTime);
}
public function GetDifferenceInDays() {
$this->difference = date_diff($this->start, $this->end);
$total = $this->difference->y * 365.25 + $this->difference->m * 30 + $this->difference->d + $this->difference->h / 24 + $this->difference->i / 60;
return ($this->difference->invert) ? -1 * $total : $total;
}
}
$obj = new DifferenceInDays('2011-01-01', '2011-01-23');
print $obj->GetDifferenceInDays() . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment