PHP : Date difference in days

Tue, Jun 17, 2008

PHP

You might need to calculate the date difference in days using PHP. You can easily calculate the days difference by using something like the code below.

The code shown above takes the UNIX timestamp of two dates and subtracts the older date from the newer date to get the number of seconds. Then we simply divide the seconds by 60 (since there are 60 seconds in a minute) to get the minutes, again by 60 (since there are 60 minutes in an hour) to get the hours and lastly by 24 (since there are 24 hours in a day) to get the number of days.

You don’t have to subtract the newer date from the older date though since we’re making use of the php abs() function which will automatically return an absolute value and eliminate a minus sign to make a negative value positive.

I hope that helps!

, , , , , , , , ,
scriptlancebannerpng

This post was written by:

Antonie Potgieter - who has written 57 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

26 Comments For This Post

  1. Kavita Says:

    Really helpful!!!!!!!!!

  2. Iain Says:

    Indeed.

    I was working on an old site that only had version 4.0 of MySQL installed so no DATEDIFF command.

    Your code above allowed me to do the calculation almost as easily in PHP.

  3. lolyz Says:

    ohh dear..itz so easy…just tell me how to get end date from starting date and number of days + time , and also eliminate the weekends..

  4. Jirka Fornous Says:

    This technique is daylight saving proof, because the one hour is everytime rounded correctly.

  5. seth odhiambo Says:

    how can i create a form that store data that is fed on it to the database and can calculate the date from which a customer logs in for booking in a hotel and on the day when he/she checks out it automatically calculates the days spent and amount to be paid and declares the room vacant

  6. Hedda Says:

    Can you show the code for the calculation of the date difference? Thank you.

  7. Moni Says:

    where is the code?

  8. luglio7 Says:

    where the hell is the coooode ? :)

  9. tast Says:

    where is the code plssssssssssss

  10. xQmail Says:

    uhm, I don’t see any code…?!

  11. Laatste Nieuws Says:

    Good thinking, this is way easier then the calculation in PHP itself…

  12. sunil Says:

    reeeeeeeeeee there no code

  13. sunil Says:

    conde is not there

  14. noneya toyou Says:

    This plays is a joke. No friggin code

  15. James - Php Development Says:

    I found your article on the following page, which contains a number of references for Php Date Time calculations.

    Calculate the difference between two Dates (and time) using Php. The following page provides a range of different methods (7 in total) for performing date / time calculations using Php, to determine the difference in time (hours, munites), days, months or years between two dates.

    See Php Date Time – 7 Methods to Calculate the Difference between 2 dates.

  16. ,m,m,.. Says:

    are you jocking with others……………..where is the code……………………..

  17. Dan Says:

    That is the lightest script I have ever seen.. or not seen…

  18. Epon Says:

    Are you people that lazy?! Look up mktime() and use simple arithmetic. Stop copy/pasting code and learn the shit yourself. There’s a reason why APIs and documentation exist.

  19. Guru Says:

    I think he forgot to insert the code. Truly he must be “lost in code” :D

  20. sssssssssss Says:

    conde is not there

  21. Radha Says:

    That is the lightest script I have ever seen.. or not seen

  22. Radha Says:

    That is the lightest script I have ever seen.. or not seenThat is the lightest script I have ever seen.. or not seenThat is the lightest script I have ever seen.. or not seen

  23. sbn Says:

    Hey,
    Where is ur code?

  24. sbn Says:

    Here the simple PHP code to get the date difference in days.It subtract the given date from current date and returns the different in days.

    function nicetime($date)
    {
    if(empty($date)) {
    return “No date provided”;
    }

    $periods = array(“second”, “minute”, “hour”, “day”, “week”, “month”, “year”, “decade”);
    $lengths = array(“60″,”60″,”24″,”7″,”4.35″,”12″,”10″);

    $now = time();
    $unix_date = strtotime($date);

    // check validity of date
    if(empty($unix_date)) {
    return “Bad date”;
    }

    // is it future date or past date
    if($now > $unix_date) {
    $difference = $now – $unix_date;

    } else {
    $difference = $unix_date – $now;
    }

    return floor($difference/(60*60*24));
    }

    $date = “2010-03-09″;
    $result = nicetime($date); // 2 days ago
    echo $result;

  25. Sandip Layek Says:

    $dateDiff = strtotime($totdate) – strtotime($frmtdate);
    $fullDays = floor($dateDiff/(60*60*24));
    echo ‘The difference between these dates is’.$fullDays;

  26. Jamp Mark Says:

    can’t find the code…
    anyways, to get the difference in days excluding time, do this
    $days = abs(strtotime(date(‘Y-m-d’,$timeTo)) – strtotime(date(‘Y-m-d’, $timeFrom)));

Leave a Reply