hello I'm trying to figure out how to compute the difference between 2 dates excluding weekends.I browsed the web on how to compute the difference between two dates and i somehow was able to understand it but i could not figure out how to compute the difference if I do not include the weekends and holidays, can some one point me to the right direction or give me some pointers for it,also if you know any good reference source it would be great thanks in advance.

I did a search but I didn't find a function or a really slick way that you can do this. It seems that you may need to loop through all of the days in your date range, determine the day of the week for each day and exclude it if it is a Saturday or Sunday. With respect to holidays, I think that you will need to create a table of holidays and then compare the date for each one with the date for each day in your date range. You will need to use the strtotime and date functions so familiarize yourself with them if you haven't used them before.

Member Avatar for diafol

You mention holidays. How do you propose to include them? Sat/Sun is easy, but holidays are usually country-specific, and even then some areas/regions within countries have different holidays.

Just be aware that 'daylight saving' can mess up date calculations based on unix time using seconds, minutes or hours as a unit of increment. I wasted a few hours puzzling why my calculations failed when the dates fell either side of 27 March 2011!

If you're using php 5.3+ there are some new classes that make this easier to accomplish.

<?php

//Define out start and end dates
$start = new DateTime('2011-01-01');
$end = new DateTime('2011-02-01');

//Define our holidays
//New Years Day
//Martin Luther King Day
$holidays = array(
	'2011-01-01',
	'2011-01-17',
);

//Create a DatePeriod with date intervals of 1 day between start and end dates
$period = new DatePeriod( $start, new DateInterval( 'P1D' ), $end );

//Holds valid DateTime objects of valid dates
$days = array();

//iterate over the period by day
foreach( $period as $day )
{
        //If the day of the week is not a weekend
	$dayOfWeek = $day->format( 'N' );
	if( $dayOfWeek < 6 ){
                //If the day of the week is not a pre-defined holiday
		$format = $day->format( 'Y-m-d' );
		if( false === in_array( $format, $holidays ) ){
                        //Add the valid day to our days array
                        //This could also just be a counter if that is all that is necessary
			$days[] = $day;
		}
	}
}

var_dump( count( $days ) );

The hardest thing to accomplish outside of php 5.3 will be creating the dates 1 at a time and dealing with month wraps etc. But, it is not impossible. The more advanced and useful solution to this would be to use two Filter Iterators on the the DatePeriod class. http://www.php.net/manual/en/class.filteriterator.php or to extend the DatePeriod class and implement the filters within itself.

PHP > 5.2 Version

Replace

//Create a DatePeriod object using a 1 day DateInterval object
$period = new DatePeriod( $start, new DateInterval( 'P1D' ), $end );

with

//Create a period array instead of using a DatePeriod/DateInterval combination
$period = array();

//Use the start and end date to fill the period array with DateTime objects increments by 1 day
while ( $start < $end ) {
	//We have to clone the start DateTime object
	//or as we increment it, every instance of it will increment
	$period[] = clone $start;
	$start->modify( '+1 day' );
}
commented: Great suggestion! +4

thanks for the reply guys this will be helpful to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.