note 95555 added to function.date

From: Date: Fri, 08 Jan 2010 18:52:18 +0000
Subject: note 95555 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-164794@lists.php.net to get a copy of this message
Date Math should not be Hard ... I messed with this to get a consistant result for far to long... Here is a function that makes date math and format output relatively simple. I am sure with additional effort it could be made Time Aware and more compact but it works for most of what I need...enjoy Function jmDateMath($SourceDate,$doMath,$ReturnFmt){ /* * ~~ jm AT trinitywebdev DOT com ~~ * * doMath Format[+/-][Qty][Unit] * * Example +6m = Add 6 Months * Example -180d = Subtract 180 Days * Example +1Y = Add 1 Year * * Unit follows date() formats * @return newday */ $SourceDate = strtotime($SourceDate); if(!$SourceDate){ return(0); } if($ReturnFmt == ''){ $ReturnFmt = 'm/d/Y'; } $m = date('m',$SourceDate); $d = date('d',$SourceDate); $Y = date('Y',$SourceDate); $MathFunc = substr($doMath,0,1); $Unit = substr($doMath,-1,1); $Qty = substr($doMath, 1,strlen($doMath)-2); if($MathFunc == '-'){ Switch($Unit){ Case "m": $newday = mktime(0,0,0,($m-$Qty),$d,$Y); break; Case "d": $newday = mktime(0,0,0,$m,($d-$Qty),$Y); break; Case "Y": $newday = mktime(0,0,0,$m,$d,($Y-$Qty)); break; } }elseif($MathFunc == '+'){ Switch($Unit){ Case "m": $newday = mktime(0,0,0,($m+$Qty),$d,$Y); break; Case "d": $newday = mktime(0,0,0,$m,($d+$Qty),$Y); break; Case "Y": $newday = mktime(0,0,0,$m,$d,($Y+$Qty)); break; } }elseif($doMath ==''){ $newday = mktime(0,0,0,$m,$d,$Y); } $newday = date($ReturnFmt,$newday); return($newday); } ---- Server IP: 192.41.40.227 Probable Submitter: 216.37.84.162 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/95555 Del: integrated -- https://master.php.net/note/delete/95555/integrated Del: useless -- https://master.php.net/note/delete/95555/useless Del: bad code -- https://master.php.net/note/delete/95555/bad+code Del: spam -- https://master.php.net/note/delete/95555/spam Del: non-english -- https://master.php.net/note/delete/95555/non-english Del: in docs -- https://master.php.net/note/delete/95555/in+docs Del: other reasons-- https://master.php.net/note/delete/95555 Reject -- https://master.php.net/note/reject/95555 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164794) next »