need to check if two dates are more than 90 days apart

I have a form that creates items on a bulletin board for a website. The form asks for a start date and end date for the announcement. Announcements may only be posted for a max of 90 days. Dates are collected as YYYY-MM-DD. I need a little routine that checks to see if the end date is more than 90 days from the start date (or approx 3 months) and if the date is more than three months, change it to be just three months from the start date.
LVL 17
nanharbisonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

khyer123Commented:
0
nanharbisonAuthor Commented:
this function told me there were 1095 days between 2009-07-03 and 2009-10-03. And I need to change the ending date if it is more than 90 days from the start date.
0
khyer123Commented:
This function might be easier to understand
function daysDifference($endDate, $beginDate)
{
 
   //explode the date by "-" and storing to array
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);
   //gregoriantojd() Converts a Gregorian date to Julian Day Count
   $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   return $end_date - $start_date;
}

Open in new window

0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Roger BaklundCommented:
Try this:
$startTS = strtotime($startDate);
$endTS = strtotime($endDate);
$maxTS = strtotime($startDate.' + 90 days');
if($endTS > $maxTS) {
  $endTS = $maxTS;
  $endDate = date('Y-m-d',$endTS);  # adjust end date
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nanharbisonAuthor Commented:
khyer123 - your function works but does not give me the new end date if the date given is more than 90 days in the future, which is the hard part for me.


cxr - I turned this into a function, see below, and then called it with the dates shown, it returns  1969-12-31, in fact no matter what end date I tried, it returned this same date. Maybe you can't just add 90 days? The whole date thing in PHP is so difficult to understand!



<?PHP
 
function daysDifference($endDate, $startDate)
{
	$startTS = strtotime($startDate);
	$endTS = strtotime($endDate);
	$maxTS = strtotime($startDate.' + 90 days');	
	if($endTS > $maxTS) {
  		$endTS = $maxTS;
	}
	$endDate = date('Y-m-d', $endTS);  # adjust end date
	return $endDate;
}
 
 
$date4 = strtotime(2009-07-03);
$date5 = strtotime(2009-07-09);
$newenddate = daysDifference($date5, $date4);
echo '<p>End date is '.$newenddate.'.</p>';
 
?>

Open in new window

0
Roger BaklundCommented:
You must use quotes for date strings, 2009-07-03 means 2009 minus 7 minus 3 = 1999. It should be like this:

$date4 = strtotime('2009-07-03');
$date5 = strtotime('2009-07-09');

...however, you should NOT use strtotime in this case, because you use that function in lines 5 and 6. So, line 16 and 17 should be like this:

$date4 = '2009-07-03';
$date5 = '2009-07-09';
0
nanharbisonAuthor Commented:
wow, that was stupid of me! I forgot I had used strtotime when I was first trying to figure this out. Thanks for a great, short solution. I would have banged away at this and come up with something long and tedious.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.