RE: [PHP] Calculating date differences using mktime

From: Date: Tue, 07 Nov 2000 19:16:56 +0000
Subject: RE: [PHP] Calculating date differences using mktime
Groups: php.general 
Request: Send a blank email to php-general+get-24162@lists.php.net to get a copy of this message
> I'm trying to figure out how to get this code I wrote to work. There > is something simple that I am not apparently grasping. Basically, I > have $now and $then, variables computed using mktime. $now is > today's date and $then is the calendar date 90 days from $now. > What I want to do is have output which reads "There are $x Months > $y Days between $now and $then". I've found a bit of info online > about similar stuff.. but nothing exactly like this. I tried DateDiff > from phpbuilder with no success. Can someone educate me briefly > on this matter to help me figure out what I'm missing? Code is > below... Thanks, Kris Try this: <script language="php"> function getDaysAndMonths( $beginTime, $endTime ) { $retval = array(); $workDate = $beginTime; $startDay = date( "d", $beginTime ); $totalMonths = 0; $totalDays = 0; $prevMonth = date( "m", $workDate ); while( $workDate <= $endTime ) { $totalDays++; $curMonth = date( "m", $workDate ); $curDay = date( "d", $workDate ); if(( $curMonth != $prevMonth ) && ( $curDay >= $startDay )) { $totalMonths++; $totalDays = 0; $prevMonth = $curMonth; } $workDate += 86400; // Add a day } $retval[months] = $totalMonths; $retval[days] = $totalDays; return $retval; } // end function getDaysAndMonths() $year=date(Y, time()); $month=date(m, time()); $today=date(j, time()); $now=mktime(0,0,0,$month,$today,$year); $then=mktime(0,0,0,$month,$today+90,$year); $totalTimeArray = getDaysAndMonths( $now, $then ); echo "There are $totalTimeArray[months] Months and $totalTimeArray[days] Days between "; echo date( "F d, Y", $now ); echo " and "; echo date( "F d, Y", $then ); </script> Chris

« previous php.general (#24162) next »