Solved

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

Posted on 2009-07-11
7
481 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 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