[Phpwm] days_between()

Phil Beynon phil at infolinkelectronics.co.uk
Thu Jun 7 11:50:12 BST 2007

This might come in useful to someone, it takes a mysql datetime field and
tells you how many days between it and either another datetime field or
formatted date() YYYY-MM-DD HH:MM:SS in a signed manner.
Its pretty flexible about data input and will return an X if it doesnt like
incoming data.

function days_between($a,$b){
$aa = explode(" ",$a); $ab = explode("-",$aa[0]);
$ba = explode(" ",$b); $bb = explode("-",$ba[0]);
if(@checkdate($ab[1],$ab[2],$ab[0]) AND @checkdate($bb[1],$bb[2],$bb[0])){
$a_bits = getdate(strtotime($a));
$b_bits = getdate(strtotime($b));
$a_new = mktime(12,0,0,$a_bits['mon'],$a_bits['mday'],$a_bits['year']);
$b_new = mktime(12,0,0,$b_bits['mon'],$b_bits['mday'],$b_bits['year']);
if($a_new > $b_new){return 0 - round(abs($a_new - $b_new)/86400);}
return round(abs($a_new - $b_new)/86400);
} else return "X";



** Infolink Electronic Systems Ltd. http://www.infolinkelectronics.co.uk
** Professional Web Design & Cobalt Hosting Solutions
** Contact: Sales at infolinkelectronics.co.uk
** Tel / Fax 0121 441 3558

More information about the Phpwm mailing list