need ur help to calculate total days ..PLZ!! ;(

elyash7 24 Aug, 2011
i need some help to calculate the total days after choosing from a certain date to a certain date using chronoforms....this is my code


<div class="form_item">
  <div class="form_element cf_heading">
    <h3 class="cf_text">Leave Application Form</h3>
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 75px;">Name</label>
    <input class="cf_inputbox" maxlength="100" size="12" title="" id="text_1" name="name_id" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 75px;">Leave Type</label>
    <select class="cf_inputbox" id="select_2" size="1" title=""  name="leavetype_id">
    <option value="">Choose Option</option>
      <option value="Annual">Annual</option>
<option value="Unplanned">Unplanned</option>
<option value="Medical">Medical</option>

    </select>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_datetimepicker">
    <label class="cf_label" style="width: 75px;">From Date</label>
    <input class="cf_datetime" title="" size="12" id="fromdate_id" name="fromdate_id" type="text" />
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_datetimepicker">
    <label class="cf_label" style="width: 75px;">To Date</label>
    <input class="cf_datetime" title="" size="12" id="todate_id" name="todate_id" type="text" />
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 75px;">Total Days</label>
    <input class="cf_inputbox" maxlength="150" size="12" title="" id="text_7" name="totaldays_id" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 75px;">Balance</label>
    <input class="cf_inputbox" maxlength="150" size="12" title="" id="text_8" name="balance_id" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label" style="width: 75px;">Reason</label>
    <textarea class="cf_inputbox" rows="3" id="text_9" title="" cols="30" name="reason_id"></textarea>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label" style="width: 75px;">Address</label>
    <textarea class="cf_inputbox" rows="3" id="text_10" title="" cols="30" name="address_id"></textarea>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 75px;">Telephone</label>
    <input class="cf_inputbox" maxlength="150" size="12" title="" id="text_12" name="telephone_id" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 75px;">Select Approver</label>
    <select class="cf_inputbox" id="select_13" size="1" title=""  name="approver_id">
    <option value="">Choose Option</option>
      <option value="En xy">En Amir</option>
<option value="En Nizar">En xz</option>

    </select>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_fileupload">
    <label class="cf_label" style="width: 75px;">Upload</label>
    <input class="cf_fileinput cf_inputbox" title="" size="12" id="file_14" name="attachment_id" type="file" />
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="button_15" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>
GreyHead 24 Aug, 2011
Hi elyash7,

What format are the dates in and where do you want to do the calculation?

Bob
elyash7 24 Aug, 2011
i wish to make it dd-mm-yyyy.....
its like this...
when a staff apply for leave application..
he will choose the Start Date and End Date with calendar view...
and i want to make it count the total days between those two....

for the calculation,its better to do in the onsubmit for confirmation page view...

thank u 4 replying😉
GreyHead 24 Aug, 2011
Hi elyash7,

This should work if you switch your date format to mm/dd/yyyy
<?php
$date_from = $form->data['fromdate_id'];
$date_to   = $form->data['todate_id'];
$date_from = strtotime($date_from);
$date_to   = strtotime($date_to);
$days = abs($date_to - $date_from);
$days = $days/(60*60*24);
$form->data['totaldays_id'] = $days;
?>


Bob

PS The strtotime format will recognise / as a separator for a US format date and - as a separator for an inernational dd-mm-yyyy format.
elyash7 25 Aug, 2011
hi bob,

thanks 4 yer help...but its still not working...
how can the total days is calculate b4 the submitting the form..
i means just like order a quantity of pizza then it will show directly the cost...
can i make it for the total days?plz help..

thank u...
GreyHead 25 Aug, 2011
Hi elyash7,

You asked for code for the OnSubmit box so that is what I wrote.

How do you know it isn't working?

Have you changed the date format to mm/dd/yyyy?

Bob
elyash7 25 Aug, 2011
Hi bob,

yes,i already try it and its not working ;(
can you help me....
i already put the code on the onsubmit area and its still not working...
have you tried?can you show me..

thank you...
GreyHead 25 Aug, 2011
Hi elyash7,

How do you know it isn't working?


Please tell me exactly what you have done and what errors you see?

Have you changed the date format to mm/dd/yyyy?


Please answer the question.

Bob
elyash7 25 Aug, 2011
1)after i put the code into onsubmit button,i try to test with my form....
it comes out nothing after submitted...i dont see any errors as i just submitted the form and the total days just went to the db with empty fields....

2)yes,i had changed it in the Data Fields setting in General tab...

Thank you for your quick reply,really appreciate it
GreyHead 25 Aug, 2011
Hi elyash7,

Please try this version with added debugging code.
<?php
$mainframe =& JFactory::getApplication();
$date_from = $form->data['fromdate_id'];
$mainframe->enqueuemessage('$date_from: '.print_r($date_from, true).'<hr />');
$date_to   = $form->data['todate_id'];
$mainframe->enqueuemessage('$date_to: '.print_r($date_to, true).'<hr />');
$date_from = strtotime($date_from);
$mainframe->enqueuemessage('$date_from: '.print_r($date_from, true).'<hr />');
$date_to   = strtotime($date_to);
$mainframe->enqueuemessage('$date_to: '.print_r($date_to, true).'<hr />');
$days = abs($date_to - $date_from);
$mainframe->enqueuemessage('$days: '.print_r($days, true).'<hr />');
$days = $days/(60*60*24);
$mainframe->enqueuemessage('$days: '.print_r($days, true).'<hr />');
$form->data['totaldays_id'] = $days;
?>


When you submit the form this should show some otuput as a Joomla! System Message. Please copy and paste it here.

Bob
elyash7 25 Aug, 2011
hi bob,

This is my result after debugging..
u can see the total days is still empty..
GreyHead 25 Aug, 2011
Hi eylash7,

Great - that helps a lot. You are using ChronoForms v3 and the code I wrote is for v4. I should have asked earlier.

Please try:
<?php
$mainframe =& JFactory::getApplication();
$date_from = JRequest::getString('fromdate_id', '', 'post');
$mainframe->enqueuemessage('$date_from: '.print_r($date_from, true).'<hr />');
$date_to   = JRequest::getString('todate_id', '', 'post');
$mainframe->enqueuemessage('$date_to: '.print_r($date_to, true).'<hr />');
$date_from = strtotime($date_from);
$mainframe->enqueuemessage('$date_from: '.print_r($date_from, true).'<hr />');
$date_to   = strtotime($date_to);
$mainframe->enqueuemessage('$date_to: '.print_r($date_to, true).'<hr />');
$days = abs($date_to - $date_from);
$mainframe->enqueuemessage('$days: '.print_r($days, true).'<hr />');
$days = $days/(60*60*24);
$mainframe->enqueuemessage('$days: '.print_r($days, true).'<hr />');
JRequest::setVar('totaldays_id', $days);
?>

Bob
elyash7 26 Aug, 2011
Thank you😉
it works....
😉
sorry 4 not telling u earlier im using v3....
is it better to use v3 or v4?
This topic is locked and no more replies can be posted.