Solved

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

Posted on 2009-07-11
7
479 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
  • 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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

22 Experts available now in Live!

Get 1:1 Help Now