Get the Number of Years Between Two Dates with PHP and JavaScript

Hero image for Get the Number of Years Between Two Dates with PHP and JavaScript. Image by Adam Tinworth.
Hero image for 'Get the Number of Years Between Two Dates with PHP and JavaScript.' Image by Adam Tinworth.

Working with dates is a pretty common task for web developers, especially developers working with dynamic content or content management systems. Often this just involves accepting a DateTime object and formatting it to appear a certain (more humanreadable) way. However, there can be much more advanced use cases for instance when building a blog and showing dates in timesince format: "1 year ago", "2 years ago", etc.

This is exactly what I do here on my own website to generate my Timeline page where I use Twitter and the #todayi hashtag to offer brief insights into my working daytoday. I've also toyed with the idea of changing my blog to display dates in this format.

So in this article, we are going to take a look at how to get the number of years between two dates, both via the backend with PHP and clientside in JavaScript.


PHP

So, to do this in PHP let's grab today's date using the DateTime class, and figure out the years between today and the 1st of January in the year 2000:

<?php  $today = new DateTime();  $millennium = new DateTime('2000-01-01');  $years_between = $millennium -> diff($today);  echo $years_between -> y;?>

When this article was published in 2014, this returned 14. The value naturally changes as the end date moves.


JavaScript

So let's take a look at doing this in JavaScript. It's a little trickier, and there are a lot of recommendations to just use libraries like moment.js (which, at over 2KB, is a pretty big dependency for what should be quite a simple operation), but here's a way to do this with just vanilla JS:

function years_between(dateFrom, dateUntil) {  var diff = (dateFrom.getTime() - dateUntil.getTime()) / 1000;  diff /= 60 * 60 * 24;  var rounded = Math.abs(diff / 365) | 0;  return rounded;}var millennium = new Date('January 1 2000');var today = new Date();console.log(years_between(millennium, today));

With JavaScript, the Date objects represent the number of milliseconds since 1st January 1970 UTC (known as Epoch or Unix time). Here, we're finding the difference between the two dates in milliseconds, converting it to days, dividing by 365, and rounding down to produce an elapsedyear estimate.

This is a much longer walk to get to the same place we achieved with four lines of PHP, but still very achievable nevertheless. There are a few minor complications if you are dealing with dates before 1970 (where Date should return a negative number). Because it divides by 365, though, this JavaScript version is only an approximation: leap years and calendar boundaries can produce the wrong fullyear count. Use a calendaraware comparison when anniversaries, ages, or legal dates need to be exact.


Postscript

July 2026: The changing result in this example was 14 when the article was published in 2014; it was never a fixed output. The PHP DateTime::diff() example uses calendar dates, whilst the JavaScript division by 365 is only approximate. For exact modern date arithmetic, Temporal is the better model once the target runtimes support it.


Untangling a delivery problem?

Send the symptoms, constraints, and affected routes. I'll help identify whether the issue sits in the application, platform, content model, deployment path, or search surface.