Skip Navigation
+44 1273 906 908 @AddedBytes
Added Bytes

PHP DateDiff Function

VBScript's DateDiff function is a powerful way to express differences between dates, and PHP lacks a similar function. Here's a replica of VBScript's DateDiff function in PHP.

Please note: This script was written some time ago and does have quirks and bugs. Several improved versions can be found in the comments on this post.

VBScript's "DateDiff" function is very useful, allowing you to quickly determine time intervals between customer dates. PHP, unfortunately, had no function to perform the same task. Instead, you must rely on a combination of the date() function and it's power and timestamps - not always the most effective way to handle dates and times.

With this in mind, the PHP DateDiff function below is intended to allow you to very quickly make the most of the power of PHP, and give you quick access to time interval functions. It is based upon the VBScript function of the same name, with a couple of small changes.

To use the below, call it like a normal function. There are four arguments. The first determines what you want to measure the date difference in - years, months, quarters, etc - and the allowed values of this are listed in the first few lines of the function. The next two are the dates themselves. Any valid date should work just fine. You can also use timestamps as dates, although if you do, you must set the last of the four arguments to "true". You can call it like so:

echo datediff('w', '9 July 2003', '4 March 2004', false);

Which will tell you the number of weekdays between the 9th July 2003 and the 4th of March 2004.


function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
    $interval can be:
    yyyy - Number of full years
    q - Number of full quarters
    m - Number of full months
    y - Difference between day numbers
        (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
    d - Number of full days
    w - Number of full weekdays
    ww - Number of full weeks
    h - Number of full hours
    n - Number of full minutes
    s - Number of full seconds (default)
    if (!$using_timestamps) {
        $datefrom = strtotime($datefrom, 0);
        $dateto = strtotime($dateto, 0);
    $difference = $dateto - $datefrom; // Difference in seconds
    switch($interval) {
    case 'yyyy': // Number of full years

        $years_difference = floor($difference / 31536000);
        if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
        if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
        $datediff = $years_difference;

    case "q": // Number of full quarters

        $quarters_difference = floor($difference / 8035200);
        while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
        $datediff = $quarters_difference;

    case "m": // Number of full months

        $months_difference = floor($difference / 2678400);
        while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
        $datediff = $months_difference;

    case 'y': // Difference between day numbers

        $datediff = date("z", $dateto) - date("z", $datefrom);

    case "d": // Number of full days

        $datediff = floor($difference / 86400);

    case "w": // Number of full weekdays

        $days_difference = floor($difference / 86400);
        $weeks_difference = floor($days_difference / 7); // Complete weeks
        $first_day = date("w", $datefrom);
        $days_remainder = floor($days_difference % 7);
        $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
        if ($odd_days > 7) { // Sunday
        if ($odd_days > 6) { // Saturday
        $datediff = ($weeks_difference * 5) + $days_remainder;

    case "ww": // Number of full weeks

        $datediff = floor($difference / 604800);

    case "h": // Number of full hours

        $datediff = floor($difference / 3600);

    case "n": // Number of full minutes

        $datediff = floor($difference / 60);

    default: // Number of full seconds (default)

        $datediff = $difference;

    return $datediff;



comments powered by Disqus

Hi! I'm Dave, a fanatical entrepreneur and developer from Brighton, UK. I've been making websites since Netscape 4 was a thing.

I built, ApolloPad and Cheatography.