Calculate Date Difference

Posted on 2007-08-04
Last Modified: 2013-11-05
I'm trying to work out date differences between two dates,
1) "today" ie. date('Y-m-d') and
2) $mydate => a date also in Y-m-d format.

I tried something like this:

$expiry_date = "2106-07-11";
$today = date('Y-m-d');
      $now_yearArr = getdate();
    $nowyear = $now_yearArr[year];
    $nowmonth = $now_yearArr[mon];

    $exp_yearArr = getdate($expiry_date);
    $expyear = $exp_yearArr[year];
    $expmonth = $exp_yearArr[mon];
      echo $nowyear;
      echo $nowmonth;
      echo "<br>";
      echo $expyear;
      echo "<br>";
      echo $expmonth;


but this outputs:
2007 <== correct
8  <==== correct
1970  <===WRONG/not what I expect===== is 2106 out of range for this function?
1 <===WRONG/not what I expect

The reason I have a date in the future is for "lifetime" members. Is the date simply out of range, or is there another way to do this.

Further down in my code, I'm checking to see if the expirey date is less than 60 days from "today" do that I can mark the date in orange; if it' 0 or less, I mark it as red.

Any help appreciated.

Question by:psimation
    LVL 20

    Expert Comment

    getdate() expects a timestamp so you would do
    $exp_yearArr = getdate(strtotime($expiry_date));

    But you can only go up to 2038 says

    Before PHP 5.1.0, negative timestamps were not supported under any known version of Windows and some other systems as well. Therefore the range of valid years was limited to 1970 through 2038.

    But i have php 5.2.1 on my local running and i still cant get pass 2038.

    A solution would be using the adodb time library
    LVL 6

    Accepted Solution

    check out this function from this page, it doesn't have the date range limitation

    in case, you are curious on what this function returns
    here's the result
    36135 (# of days)
    and if you divide the result by 365 days, you'll get
    99 (# of years)

    === code (make sure you get the function from the above link) ===
    $expiry_date = "07/11/2106";
    $today = date('m/d/Y');

    echo dateDiff("/",$expiry_date,$today); // # of days
    echo "<br />";
    echo (dateDiff("/",$expiry_date,$today))/365; // # of years
    === code ===

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now