Compare 2 dates

Hi guys, need a little help if possible
I have 2 dates, a start date and an end date, (“acc2” and “acc3”) which are in DD/MM/YYYY format, acc3 is always going to be bigger/later than acc2

what I’m wanting to do is figure out the time difference between them and if it’s greater than 6 weeks (42 days) then give an error like the example:

if(document.all.[b]accX[/b].value.length >42)
{
alert("Accounts can only be a maximum of 6 weeks in length");
return false;
}

I have no idea how to get from what I’ve got to what I need, any help appreciated

Are you trying to do it with javascript or php? if its php, use strtodate($start_date, “+6 weeks”); that will give you 6 weeks from the start date.

Hi mate, I’m trying to do it in PHP, the user can select any 2nd date UPTO 6 weeks after the first date, so what I’m wanting is basically, if date 2 (acc4) minus date 1 (acc3) equals more than 42 days difference then I want to give a warning
the dates are in d/m/Y format (e.g. today is 5th of July so 05/07/2012)

I’m still learning PHP on the job by picking up parts of code and altering it, the 2 things I’ve tried so far are:

[php] <?php
$acc3 = strtotime($from_date);
$acc4 = strtotime($to_date);
if ($to_date - $from_date >=43)
{
echo “more than 42 days”;
}
?>[/php]
and
[php] <?php
function
compare_dates($acc3, $acc4)
{
list($day, $month, $year) = preg_split(’/’, $acc3);
$from_date = sprintf(’%04d%02d%02d’, $year, $month, $day);

		  list($day, $month, $year) = preg_split('/', $acc4);
		  $to_date = sprintf('%04d%02d%02d', $year, $month, $day);

		  if ($to_date - $from_date >=43) 
		  {
		  echo "more than 42 days";
		  }
		  }
		  ?>[/php]

OK, think it’s a problem with the date, I just added [php]<?php echo($acc3); ?>[/php] to the page and it puts “1341442800” on the page and that doesn’t change if I change the date, back to the drawing board

But you don’t need to have them do that. Have them select the first date, then auto generate the second date. have a read through http://thetricky.net/php/Compare%20dates%20with%20PHP - a very easy and fast way of comparing 2 dates. You’d have to do an if statement to adjust for a leap year though.

I can’t auto generate a 2nd date as it can be any time UP TO 6 weeks, for example, today they can put todays date as the start date, and todays date as the end date, but if they choose a date that’s more than 6 weeks after the 1st date then I need it to throw up an alert or even better add a line in to the form with a link.

if you read through that tutorial, it tells you how to do that exact thing. its on the bottom of the page :slight_smile:

OK, from that I’ve come up with:
[php] <?
$acc2 = date(“d/m/Y”);
$acc3 = date(“d/m/Y”);
$from_date = strtotime($acc2);
$to_date = strtotime($acc3);
if ($to_date - $from_date >= 43)
{ ?>
over 42 days
<? } ?>[/php]
I’m doing something wrong, not sure what though

You’ve gotta do the math before you compare, and don’t use the short tags, not all hosts allow it.
[php]<?php
$acc2 = date(“d/m/Y”);
$acc3 = date(“d/m/Y”);
$from_date = strtotime($acc2);
$to_date = strtotime($acc3);

if ($to_date - $from_date) >= 43) {
echo “over 42 days”;
}
?>[/php]
One question though, if they’re choosing the start and end dates, where are the _POST inputs from the form? shouldn’t it be
[php]<?php
$acc2 = date(“d/m/Y”, $_POST[‘start’]);
$acc3 = date(“d/m/Y”, $_POST[‘end’]);
$from_date = strtotime($acc2);
$to_date = strtotime($acc3);

if ($to_date - $from_date) >= 43) {
echo “over 42 days”;
}
?>[/php]

not sure what short tags are or what maths I need to do
the form is altered from what we already had and works fine, not sure what the POST bits are but as the form stands it’s not needed, cheers

I’m not getting the whole story then. you said you had a form where the user selected 2 dates, a starting date and an ending date that had to be less than 6 weeks (which is 90 days, not 42). What you showed us doesn’t involve any user input, its just taking today’s date (twice), and seeing if its greater than or equal to 43.

The math is the date subtraction, which you have reversed btw. It can’t be a negative number, just put the from date in front and it should be good on that front.

Hi mate, sorry if I’ve confused
the bit in the form where they input the date is:

<p class="quest">Date from: <input type="text" name="acc2" class="tcal" value="" /> to: <input type="text" name="acc3" class="tcal" value="" /> it uses a javascript calendar and gives 2 strings DD/MM/YYYY, a “start date” (acc2) and a “finish date” (acc3)
these dates cannot be more than 6 weeks in length (6x7 = 42, 90 days is 12.8 weeks), if they enter dates too far apart then I want to either:
a) warn them as soon as they select the dates and give them a link to a form they need to fill in
b) a pop-up alert on form submission saying we cannot accept it as the term is too long

acc3 minus acc2 will give a positive number as acc3 is in the future and therefore a larger number than acc2 (I think)

your help is very much appreciated!

That helps some, i was right. You’re right about the math though, its what i get for looking at this at 5am. So those date fields are filled in from the javascript then? If you want the notification immediately, then you either need to use js or an ajax script. There’s lots of tutorials on js data comparison, but i found this on stackoverflow

function d_check() { 
    var dl_sdt=document.getElementIdBy("date_input_Id1").value; //date one 
    var dl_endt=document.getElementIdBy("date_input_Id2").value; //date two 
 
    if((dl_sdt.substr(6,4)) > (dl_endt.substr(6,4))) { 
        alert("first date is greater"); 
        return false; 
    } 
 
    else if((((dl_sdt.substr(0,2)) > (dl_endt. 
        substr(0,2)))&&(frdt(dl_sdt.substr(3,2)) > (dl_endt.substr(3,2))))|| 
        (((dl_sdt.substr(0,2)) > (dl_endt.substr(0,2)))&& 
        ((dl_sdt.substr(3,2)) < (dl_endt.substr(3,2))))|| 
        (((dl_sdt.substr(0,2)) == (dl_endt.substr(0,2)))&&((dl_sdt.substr(3,2)) >  
        (dl_endt.substr(3,2))))) { 
            alert("first date is greater"); 
        return false; 
    } 
 
    alert("second date is digher"); 
    return true; 
}[/code]

If you wanted to use ajax, then you'd just put what you have in a seperate page and pass the variables via the url string.  something like
[code]<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//document.myform.time.value = ajaxRequest.responseText;
			var ajaxDisplay = document.getElementById('n_answer');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var a_id = document.getElementById('danswer').value;
	var queryString = "?a_id=" + a_id;
	ajaxRequest.open("GET", "ajax_query.php" + queryString, true);
	ajaxRequest.send(null);
}

//-->
</script>

That’s from one of my projects. Its for querying a database, but it can be changed to suit your needs easily enough.

thank you very much, I’ll give it a go in a bit
thank you for all your help with this

I couldn’t get that going, but I’m getting closer now, I have the following Javascript, the problem is that it only checks the days difference as soon as the page loads (which is always going to be 0), can I get it to check once the dates have changed?

[code][/code]

Use the first javascript, you’ll need to add id’s to your date inputs (id=“acc2”, etc), change what’s in
var dl_sdt=document.getElementIdBy(“date_input_Id1”).value; //date one
var dl_endt=document.getElementIdBy(“date_input_Id2”).value; //date two
to match your stuff (change date_input_Id1 & 2)

then in your second date, add onChange=“d_check();”, also change the messages where it returns false to match what you need.

Sponsor our Newsletter | Privacy Policy | Terms of Service