CalcBuilder Forum

Count number of days

Usuario phil_adler 2016-02-02 17:33:42

I need some help in figuring out the formula for counting the number of days between 2 dates

Moonsoft support 2016-02-02 17:58:55

Hi,
yes, we can provide that one. We assume you have created a couple of date fields at your form, please share which is the date format you're using (the one you've set at Preferences->front end date format) for your calculator.

Regards

Usuario phil_adler 2016-02-02 19:16:13

mm/dd/yy

Yes I have 2 date fields start date and end date

Edited by phil_adler - 02.02.2016 20:16

Moonsoft support 2016-02-03 11:58:12

Hi,
ok, assuming your field dates variables are called 'startdate' and 'endddate', the code would be:

$format="m/d/Y";

$date1 = date_create_from_format($format,$startdate);
$date2 = date_create_from_format($format,$endddate);
$diff = date_diff($date1, $date2);

$days = $diff->d;

Now you have the difference at $days, that you can use for further calculations or print at the exit layout with ##days##

Please note those date functions above are valid starting from php 5.3, if you have an older version let us know so we'll provide another approach to get this result.
Please let us know if you need more help with this.
Regards

Usuario phil_adler 2016-02-03 16:10:22

perfect thank you very much. Now what would be the best way to add some css to it so it does not look like just some fields on a webpage

Moonsoft support 2016-02-03 17:34:31

Hi,
you can enable bootstrap classes from the preferences tab, that will make the fields to adopt the bootstrap look and feel, otherwise they will be inheriting the styles from your template. If you want to add some more styles to the form, you can use the 'form layout' html editor, and also the js tab 'write directly in html page' if you want to include any external css file or style tag (this tab works including all its content inside the page)


Hope this helps, regards

Usuario phil_adler 2016-02-08 17:10:24

I need to add 1 day to the calculation. I have used the $date2->modify('1 Day');

but the issue is I am adding a possible of 5 lines so when I add that code to each calculation the total returns 1 even if no dates are selected how do I fix this ?

Moonsoft support 2016-02-08 17:25:29

Hi,
the reason is that you're adding one day to 'today'. If you don't want to show a result when any of the dates is empty, you can check this post regarding same question:



https://www.moonsoft.es/en/help/2/3299-inline-day-calculator

Regards