• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4535
  • Last Modified:

Calculate Date Difference

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.

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

But you can only go up to 2038

php.net 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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now