Solved

# Calculate Date Difference

Posted on 2007-08-04
4,473 Views
HI
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:

<?php
\$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"<br>";
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.

0
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

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

0

LVL 6

Accepted Solution

check out this function from this page, it doesn't have the date range limitation
http://www.developertutorials.com/tutorials/php/calculating-difference-between-dates-php-051018/page1.html

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 ===
0

## Featured Post

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.