2

I've been thrown in at the deep end at work, I grew up knowing plain HTML (mid-90's) and I've been asked to alter a form at work that's in PHP, I can alter PHP code written by others, but there's no way I could write it myself. I've altered this form to just about how I want it, but there's 1 bit I want to alter, if anyone can help it would be greatly appreciated...

been on this for 2 days searching the web and trying stuff, I have the following

 <p class="quest">Date from: <input type="text" name="acc2" class="tcal" value="" /> to: <input type="text" name="acc3" class="tcal" value="" />

it uses javascript to pop up a calendar (tigra calendar) that then returns 2 dates (acc2 and acc3), the dates are in d/m/Y (DD/MM/YYYY) format (standard here in Europe/Aus/NZ). What I'm wanting to do is get it to check if the difference is greater than 42 days (6 weeks), if it is I want it to display some text with a link (42 days or less is fine). If I put on the page as a test:

<?php echo($acc3); ?>

then it displays 1341442800 on the page (before a date is chosen), if the user selects a date then the above number stays (not dynamic)

Is what I'm wanting possible? If not, can it be checked on submission and pop up an alert (without the link)? I currently use Javascript to check the form, an example is:

<script language="javascript" type="text/Javascript">
    <!--
        function collect()  {
            if(document.all.ct.value == "TempAcc")
            {
                if(document.all.acc7.value.length <11)
                {
                    alert("You need to fill in your contact number with your FULL number including STD code.");
                    return false;
                }
            }
        }//end function collect
    //-->
    </script>

any help with this would be very much appreciated, please try to be specific with any code as I'm am still very much a beginner. Thanks in advance.

Maff
  • 441
  • 1
  • 6
  • 27

3 Answers3

1

That works for me: Live demo on fiddle.net

// parse dates from DD/MM/YYYY to YYYY/MM/DD
var oldBegin = parseDate("03/06/2012");
var oldEnd = parseDate("07/06/2012");

// calculate difference and format to days
var diff = Math.round((oldEnd - oldBegin)/1000/60/60/24);
alert(diff);


function parseDate(str) {
  var m = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
  return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}

Edit: Include jQuery to your webpage. It will help you with the javascript part. Give both of your input-fields a unique id (like id="date1" and id="date2") and then get the values in javascript

$('date1').val();
$('date2').val();

The whole code should look something like that:

<script type="text/javascript">
//The code will trigger when the user leaves the second input field
$("#date2").focusout(function() {
    // parse dates from DD/MM/YYYY to YYYY/MM/DD
    var date1 = parseDate($('date1').val());
    var date2 = parseDate($('date2').val());

    // calculate difference and format to days
    var diff = Math.round((date2 - date1)/1000/60/60/24);
    alert(diff);


    function parseDate(str) {
      var m = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
      return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
    }
});
</script>

You can now add some if-statements to check if one input is empty.

Chris
  • 4,255
  • 7
  • 42
  • 83
  • Hi Chris, I just tried this, loading the page brings up the following error Parse error: syntax error, unexpected T_VAR in C:\Program Files\Hornbill\Supportworks Server\html\_selfservice\selfservice\tempacc.php on line 35 this is the code I used: var acc3 = parseDate("03/06/2012"); var acc2 = parseDate("07/06/2012"); // calculate difference and format to days var diff = Math.round((acc3 - acc2)/1000/60/60/24); alert(diff); function parseDate(str) { var m = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/); return (m) ? new Date(m[3], m[2]-1, m[1]) : null; } ?> – Maff Jul 05 '12 at 08:36
  • It's because you need to wrap the code in ` – Chris Jul 05 '12 at 08:39
  • Cheers chris, this pops up an alert of "4" when the page loads, any idea how I can compare once they have entered the dates in the form rather than the dates given in your example? – Maff Jul 05 '12 at 09:04
  • Check my edit. I really don't want to write the whole code for you, you better check some basic javascript tutorials. Cheers! – Chris Jul 05 '12 at 09:22
  • Thank you Chris, kind of got it working now, I have got my if statement in there, the thing is, it's not checking the dates once they are entered, only when the page is loaded (no dates entered so difference is 0) - as a test I have: if (diff>=43) { document.write("too long") } – Maff Jul 05 '12 at 09:42
  • If you want to check the first date when entered: `$("#date1").focusout(function() { //if statetment here });` If my answer is fine for you, please accept it. – Chris Jul 05 '12 at 10:26
0

Here's two questions/answers written in PHP and Javascript

PHP

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

Javascript

var oldBegin = ...
var oldEnd = ...
var newBegin = ...

var newEnd = new Date(newBegin + oldEnd - oldBegin);
Community
  • 1
  • 1
Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
0

Try this :

<script type="text/javascript">
function daysBetween() {
    var one = new Date(2012, 0, 1); // YYYY - MM - DD
    var two = new Date(2012, 5, 5); // YYYY - MM - DD

    var one_day=1000*60*60*24

   document.write(Math.ceil((two.getTime()-one.getTime())/(one_day))+" days")
}
</script>
Ravinder Singh
  • 3,113
  • 6
  • 30
  • 46
  • Cheers ravinder, I've added this to the page (and tried changing "one" to "acc2" and "two" to "acc3" but it doesn't do anything – Maff Jul 05 '12 at 08:34
  • – Maff Jul 05 '12 at 09:01
  • Inside the document.write, change two and one to acc2 and acc3 also. – Ravinder Singh Jul 05 '12 at 09:50