note 35883 deleted from function.mktime by sniper

From: Date: Tue, 04 Nov 2003 13:09:48 +0000
Subject: note 35883 deleted from function.mktime by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-59862@lists.php.net to get a copy of this message
Note Submitter: graeme@perrow.ca ---- Here's a function that returns a text string giving the amount of time since a particular date. For example, on Sept 17, 2003, howLongSince( 6, 4, 1999 ) (April 6, 1999) returned "4 years, 5 months, and 11 days". I originally got it from javascript.internet.com, and ported it from Javascript to PHP. function howLongSince( $startdate, $startmonth, $startyear ) { $sdate=$startdate; $smonth=$startmonth; $syear=$startyear; $DaysInMonth = array(0,31,28,31,30,31,30,31,31,30,31,30,31); $thisyear = date( 'Y' ); $thismonth = date( 'n' ); $thisdate = date( 'j' ); $mstart = mktime(0, 0, 0, $smonth==12 ? 1 : $smonth+1, 1, $syear ); $days1 = ($mstart - mktime( 0, 0, 0, $smonth, $sdate, $syear))/(24*60*60)-1; $mend = mktime( 0, 0, 0, $thismonth, 1, $thisyear ); $days2 = (mktime( 0, 0, 0, $thismonth, $thisdate, $thisyear ) - $mend)/(24*60*60)+1; $dayst = $days1 + $days2; if ($dayst >= $DaysInMonth[$smonth]) { $AddOneMonth = 1; $dayst -= $DaysInMonth[$smonth]; } else $AddOneMonth = 0; $ydiff1 = $thisyear-date('Y', $mstart ); $mdiff1 = $thismonth-date('n', $mstart)+$AddOneMonth; $dayst = floor($dayst); if ($mdiff1 > 11) { $mdiff1=0; $ydiff1++; } if ($mdiff1 < 0) { $mdiff1 = $mdiff1 + 12; $ydiff1--; } if ($ydiff1 == 0 ) { $temp = ""; if ( $mdiff1 != 0 ) { $temp .= $mdiff1==1? "1 month, and " : "$mdiff1 months, and "; } $temp .= $dayst==0 ? "0 days" : $dayst==1 ? "1 day" : "$dayst days"; } else { $temp = ""; if( $ydiff1 > 0 ) { $temp .= $ydiff1==1 ? "1 year, " : "$ydiff1 years, "; } if( $mdiff1 > 0 ) { $temp .= $mdiff1==1 ? "1 month, and " : "$mdiff1 months, and "; } $temp .= $dayst==0 ? "0 days" : $dayst==1 ? "1 day" : "$dayst days"; } return $temp; }

« previous php.notes (#59862) next »