[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-07-11
7
Medium Priority
?
490 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:nanharbison
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:khyer123
ID: 24832939
0
 
LVL 17

Author Comment

by:nanharbison
ID: 24832954
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
 
LVL 4

Expert Comment

by:khyer123
ID: 24832971
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24832998
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
 
LVL 17

Author Comment

by:nanharbison
ID: 24833933
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
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24833985
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
 
LVL 17

Author Closing Comment

by:nanharbison
ID: 31602511
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

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 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