July 5th, 2007 at

PHP - calculate days or nights from two dates using php

in: Scripts

This function returns the difference of two dates in whole days.  This is great for such uses as hotel calendars to work out how many nights someone would be staying or to give a count down to a specified date.

The input needs two dates; the start ($Date1) and finish ($Date2). 

 This function uses the php mktime function.

The date input is yyyy/mm/dd e.g. 2008/01/29

######## start script

function getDayDifference($Date1, $Date2) {

// script by Richard Wilkins
// www.icomtek.co.uk

// explode the dates into their years,months,days
$expDate1 = explode("-", $Date1);
$expDate2 = explode("-", $Date2);

// do the calculation
$days = (int)((mktime (0,0,0,$expDate2[1],$expDate2[2],$expDate2[0]) - mktime(0, 0, 0, $expDate1[1],$expDate1[2],$expDate1[0]))/86400);

// return the result
return $days;

}

// example use
echo getDayDifference("2008-01-01", "2008-01-10");

######## finish script

Download the full script here.

 

RSS feed for comments on this post | TrackBack URI