This topic is locked

Guide 21 - Calculation of days between two dates

7/4/2021 5:52:50 PM
PHPRunner Tips and Tricks
fhumanes author

This example I hope you help you something very common and it is calculate the days between two dates.

This calculation is given in many systems (holiday management, hotel reservation, etc.). The calculation of days between two dates is a direct operation, do not know the types of days between the dates, that is, if the days are:

  • Natural days
  • Work days
  • Weekend (Saturdays and Sundays)
  • Holidays


Calculate the days between two dates, identifying if they are (natural, working, weekend and holidays).


You can use users admin and user1. The Password is the same as the Login.

All the example can be downloaded from my portal

fhumanes author 7/4/2021


The example is developed in Phprunner 10.2 to reach more developers and their appearance is:

img alt
A table has been enabled to indicate the festivals of the organization. Only users with "admin" profile will be able to keep the information on this table.

img alt
The festive table is a record for each holiday. It has been indicated through calendar that can not be a day from Saturday or Sunday, so that the holidays and the weekend can not be duplicated,

img alt
The calculated days are displayed in the image according to the types of days that are in the range of the dates (initial and final) that are reported.

In the example there are validations at JavaScript and PHP level. The codes for the EDIT action will be displayed, because it is something more complex than ADD.

The event: JavaScript Onload Event:

var ctrl_date_init = Runner.getControl(pageid, 'date_init');
var ctrl_date_end = Runner.getControl(pageid, 'date_end');

this.on('beforeSave', function(formObj, fieldControlsArr, pageObj){
var init = ctrl_date_init.getValue();
var end = ctrl_date_end.getValue();
if ( init > end ) {
alert("It is necessary that the initial date is less than or equal to the end date");
Runner.delDisabledClass(pageObj.saveButton ); // Activate again, button SAVE
return false;
} else {
return true;

The event: Before Record Updated:

$id_user = $values['date_user_id'];
$Dinit = $values['date_init'];
$Dend = $values['date_end'];
$id = $keys['id_date_permissions'];

// Control of the dates
if ( $Dinit > $Dend ) {
$message = "The initial date must be equal to or less than the final date";
return false;

// Retrieve the holidays of the period
$holidays = array();
$rs = DB::Query("SELECT * FROM date_holidays where holiday between '$Dinit' and '$Dend'");
while( $data = $rs->fetchAssoc() )
$holidays[] = $data['holiday'];

// Control of afforded dates
$rs = DB::Query(
"SELECT * FROM date_permissions
where `date_user_id` = $id_user and `id_date_permissions` <> $id
and (( date_init >= '$Dinit' and date_end <= '$Dend')
or (date_init < '$Dinit' and date_end >= '$Dinit' )
or (date_init > '$Dinit' and date_init <= '$Dend'))"
while( $data = $rs->fetchAssoc() )
$message = "Some days of this range are already requested";
return false;
// Count the days of between dates
$count_N = 0;
$count_B = 0;
$count_H = 0;
$count_W = 0;

$start = strtotime($Dinit);
$end = strtotime($Dend);
while(date('Y-m-d', $start) <= date('Y-m-d', $end)){
$count_N += 1;
$string_init = date('Y-m-d', $start);
if (in_array($string_init, $holidays)) {
$count_H += 1; // Is holiday
} else {
$d_w = date('N', $start);
$count_B += $d_w < 6 ? 1 : 0; // Is Business
$count_W += $d_w < 6 ? 0 : 1; // Is weekend
$start = strtotime("+1 day", $start);

$values['natural_days'] = $count_N;
$values['business_days'] = $count_B;
$values['holidays'] = $count_H;
$values['weekdays'] = $count_W;

return true;

For any questions or any other need, contact Email

I leave you in my portal the example so that you can download it in your Windows and you can perform the tests and changes you need.

Salaheddin 7/5/2021

Thank you Vmuch for your efforts