note 35883 added to function.mktime

From: Date: Thu, 18 Sep 2003 02:01:52 +0000
Subject: note 35883 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-56668@lists.php.net to get a copy of this message
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; } ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35883 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35883&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35883&report=yes

« previous php.notes (#56668) next »