1

It's the equivalent of the MySQL to_days() function.

Is there a builtin PHP function that does this, or do I need to cobble something together?

John Conde
  • 217,595
  • 99
  • 455
  • 496
jodonnell
  • 49,859
  • 10
  • 62
  • 67

2 Answers2

8

You'd need to write your own but it's not hard:

$now = new DateTime();
$zero = new DateTime('0000-00-00'); // -0001-11-30 - Nov 30, 1 BC. Interesting.
$diff = $now->diff($zero);
echo $diff->format('%a days'); // 735728 days

Demo using the literal year zero. You obviously would want to put a valid date in there instead.

$now = new DateTime();
$zero = new DateTime('0001-01-01'); 
$diff = $now->diff($zero);
echo $diff->format('%a days'); // 735330 days

Demo

As a one liner:

echo (new DateTime())->diff(new DateTime('0001-01-01'))->format('%a days');

As a function:

function toDays($date) {
    return (new DateTime())->diff(new DateTime($date))->format('%a');
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

You can use the Julian day count, i.e. with cal_to_js(), see http://www.php.net/manual/de/function.cal-to-jd.php, even if there was no year 0 in the Gregorian calendar.

VMai
  • 10,156
  • 9
  • 25
  • 34