note 51561 added to function.date

From: Date: Mon, 04 Apr 2005 11:39:53 +0000
Subject: note 51561 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-87423@lists.php.net to get a copy of this message
When I try to find date array between two given dates as difference; There is no functionality available Presently. So, I developed one function for it. THIS FUNCTION IS USED TO GET THE DATE ARRAY FROM GIVEN TWO DATES...$fromdate TO $todate..Date Format Should be(YYYY-MM-DD) function GetDateArray($fromdate, $todate){ //Let's Set The FromDate Here $fromd = sscanf("$fromdate", "%d/%d/%d"); //Let's Set The ToDate Here $tod = sscanf("$todate", "%d/%d/%d"); //Let's Get The Year Difference Here $yrdiff = $tod[2]-$fromd[2]; $toyear = $tod[2]; $fromyear = $fromd[2]; //Let's Get The Month Difference Here $mondiff = $tod[1]-$fromd[1]; $tomonth = $tod[1]; $frommonth = $fromd[1]; //Let's Get The date Difference Here $datediff = $tod[0]-$fromd[0]; $today = $tod[0]; $fromday = $fromd[0]; if($yrdiff >= 0){ //Month Set Here if($mondiff < 0) $mondiff1 = $mondiff + 12 - 1; else if ($mondiff >= 0 and $yrdiff > 0) $mondiff1 = $mondiff+12*$yrdiff; else $mondiff1 = $mondiff; //Let's Set The Days, Moth And Year Here $days = 0; $mon = $fromd[1]; $yr = $fromd[2]; if($mondiff1>1){ for($i=1 ; $i<=($mondiff1+1) ; $i++ ){ if($i != 1) $mon = $mon+1; if($mon>12){ $mon=1; $yr =$yr+1; } if($frommonth == $mon && $yr == $fromyear) $fromd = $fromday; else $fromd = 1; if($tomonth == $mon && $yr == $toyear) $days = $today; else $days = date(t,mktime(0,0,0,$mon,1,$yr)); for($j = $fromd; $j<=$days; $j++){ $arrDate[] = $yr."-".$mon."-".$j; } } }else if($mondiff1 == 1){ for($i=1 ; $i<=($mondiff1+1) ; $i++ ){ if($i != 1) $mon = $mon+1; if ($mon>12){ $mon=1; $yr =$yr+1; } if($frommonth == $mon && $yr == $fromyear) $fromd = $fromday; else $fromd = 1; if($tomonth == $mon && $yr == $toyear) $days = $today; else $days = date(t,mktime(0,0,0,$mon,1,$yr)); for($j = $fromd; $j<=$days; $j++){ $arrDate[] = $yr."-".$mon."-".$j; } } }else{ if($frommonth == $mon && $yr == $fromyear) $fromd = $fromday; else $fromd = 1; if($tomonth == $mon && $yr == $toyear) $days = $today; else $days = date(t,mktime(0,0,0,$mon,1,$yr)); for($j = $fromd; $j<=$days; $j++){ $arrDate[] = $yr."-".$mon."-".$j; } } } return $arrDate; } eg: $dateArr = GetDateArray("1/3/2003", "15/3/2005"); ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51561 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51561&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51561&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51561&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51561&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51561&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51561&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87423) next »