Solved

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

Posted on 2009-07-11
7
480 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

20 Experts available now in Live!

Get 1:1 Help Now