| I do a lot of business application programming. Accounting deals a lot with dates so my first batch of functions will more than likely be Date and Numeric functions because I use them a lot. The DateAdd function is really useful when building reports. For those that are familiar with ColdFusion, I do not use a few of the defaults in the ColdFusion version of this function. |
|
|
|
|
| The Function: |
| <?php function DateAdd($datepart,$action,$number, $date) { $adbit = explode(" ",$date); $bdbit = explode("-",$adbit[0 ]); if(count($adbit) > 1) { $cdbit = explode(":",$adbit[1]); } else { $cdbit = array(); $cdbit[0] = 0; $cdbit[1] = 0; $cdbit[2] = 0; } switch($datepart) { case "l": // Millisecond (Lower case 'L') $e = 60/1000; break; case "s": // Second $e = 1 ; break; case "n": // Minute $e = 60; break; case "h": // Hour $e = 60*60; break; case "ww": // Week $e = ((60*60)*24)*7; break; case "d": // Day $e = (( 60*60)*24); break; case "m": // Month $e = (((60* 60)*24)*365)/12; break; case "yyyy": // Year $e = (((60* 60)*24)*365); break; default: $e = "error"; } if($e == "error") { return false; } $intTime = mktime($cdbit[0],$cdbit[1],$cdbit[ 2],$bdbit[1],$bdbit[2],$bdbit[0]); if($action == "+") { $nTime = $intTime + ($e * $number); } else { $nTime = $intTime - ($e * $number); } return date( "Y-m-d H:i:s",$nTime); } ?> |
| Usage: |
The DateAdd function can be used to add or subtract a numeric value from a date. Below is an example of how to add 7 days. // Add 7 days to May 19th $action = "+"; // Subtract 7 days from May 19th $action = "-"; |
Monday, December 24, 2007
DateAdd
Wednesday, December 19, 2007
DateDiff
| The DateDiff function has been another useful function in report building. It can be useful when trying to determine the average time for each stage of development in a project, or the amount of time that it took to complete. I have used it enough in both php and ColdFusion applications. I have found that using a consistent method of calculating dates is worth its weight in gold in the long run! | ||||||||
|
The Function
<?php
function DateDiff($datepart, $date1, $date2)
{
$a = explode(" " ,$date1);
$b = explode("-",$a[0]);
if(count($a) > 1)
{
$c = explode(":",$a[1]);
}
else
{
$c = array();$c[ 0] = 0;$c[1] = 0;$c[2] = 0;
}
$a1 = explode (" ",$date2);
$b1 = explode("-",$a1[0]);
if(count($a1) > 1)
{
$c1 = explode(":",$a1[1]);
}
else
{
$c1 = array();$c1[ 0] = 0;$c1[1] = 0;$c1[2] = 0;
}
switch($datepart)
{
case "n": // Minute
$db= 60;break;
case "h": // Hour
$db= 60*60;break;
case "d": // Day
$db=(60*60)*24;break;
case "w": // Weeks
$db=((60*60)*24)* 7;break;
case "m": // Month
$db=ceil(((( 60*60)*24)*365)/12);break;
case "yyyy": // Year
$db=(((60*60)*24)* 365);break;
default:
$db=1;
}
$nDate1 = mktime($c[0],$c[ 1],$c[2],$b[1],$b[2],$b[0]);
$nDate2 = mktime($c1[ 0],$c1[1],$c1[2],$b1[1],$b1[2],$b1[0]);
$rDate = $nDate1 - $nDate2;
$num = number_format(($rDate/$db),4);
if($num < 0)
{
return $num * -1;
}
else
{
return $num;
}
}
?>
<?php
$datepart = "s";
$date1 = "2007-12-01 19:20:12" ;
$date2 = "2007-11-15 11:45:39";
echo DateDiff($datepart, $date1, $date2);
?>
The result should be: 1,409,673!
<?php
$datepart = "yyyy";
$date1 = "2007-12-01 19:20:12" ;
$date2 = "2007-11-15 11:45:39";
echo DateDiff($datepart, $date1, $date2);
?>
The result should be: 0.0447!
Tuesday, December 18, 2007
DateCompare
| The DateCompare function has been a very useful tool when building reports. By default, this function will compare dates down to the second. With a little bit of tweaking, you can have it compare down to the nano second. By sending a datepart in, you can specify the level of percision that you want. | ||||||||
|
<?php
function DateCompare($date1, $date2=date("Y-m-d H:i:s"), $datepart)
{
$a = explode(" ",$date1);
$b = explode("-",$a[ 0]);
if(count($a) > 1)
{
$c = explode(":",$a[ 1]);
}
else
{
$c = array();$c[0] = 0;$c[1] = 0;$c[2] = 0;
}
$a1 = explode(" ",$date2);
$b1 = explode("-",$a1[0]);
if(count($a1) > 1)
{
$c1 = explode(":",$a1[1]);
}
else
{
$c1 = array();$c1[0] = 0;$c1[1] = 0;$c1[2] = 0;
}
switch($datepart)
{
case "n": // Minute
$c[0] = 0;$c1[0] = 0;break;
case "h": // Hour
$c[0] = 0;$c[1] =0;$c1[0] = 0;$c1[1] = 0;break;
case "d": // Day
$c[ 0] = 0;$c[1] = 0;$c[2] = 0;$c1[ 0] = 0;$c1[1] = 0;$c1[2] = 0;break;
case "m": // Month
$c[0] = 0;$c[ 1] = 0;$c[2] = 0;$c1[0] = 0;$c1[ 1] = 0;$c1[2] = 0;$b[1] = 0;$b1[1] = 0;break;
case "yyyy": // Year
$c[ 0] = 0;$c[1] = 0;$c[2] = 0;$c1[0] = 0;$c1[1] = 0;$c1[2] = 0;$b[1] = 0;$b1[ 1] = 0;$b[2] = 0;$b1[3] = 0;break;
default:
$e = "mc2";
}
$nDate1 = mktime($c[0 ],$c[1],$c[2],$b[1],$b[2],$b[0]);
$nDate2 = mktime($c1[0],$c1[1],$c1[2],$b1[1],$b1[2],$b1[0 ]);
if($nDate1 < $nDate2)
{
return -1;
}
elseif ($nDate1 > $nDate2)
{
return 1;
}
else
{
return 0;
}
}
?>
Usage:
Using the DateCompare function is easy. If date 1 is greater than date 2, the function will return 1. If date 1 is less than date 2, the function will return a -1. If the two dates are equal, the function will return a 0.
<?php
$date1 = "2007-05-01 15:07:54" ;
$date2 = "2007-06-07 02:41:22";
if(DateCompare($date1,$date2) == 1)
{
echo "Date 1 is GREATER THAN Date 2";
}
elseif (DateCompare($date1,$date2) == -1)
{
echo "Date 1 is LESS THAN Date 2";
}
else
{
echo "Date 1 EQUALS Date 2" ;
}
?>
Sunday, December 16, 2007
DateFormat
The Function:
<?php
function DateFormat($date,$format)
{
$adbit = explode(" ",$date);
$bdbit = explode("-",$adbit[0]);
if(count($adbit) > 1)
{
$cdbit = explode(":",$adbit[1]);
}
else
{
$cdbit = array();
$cdbit[0] = 0;
$cdbit[1] = 0;
$cdbit[2] = 0;
}
if(strtolower($format) == "short")
{
$dformat = "m/d/Y";
$tformat = "H:i a";
}
elseif(strtolower($format) == "medium")
{
$dformat = "M d, Y";
$tformat = "H:i:s a";
}
elseif(strtolower($format) == "long")
{
$dformat = "F d, Y";
$tformat = "H:i:s a e";
}
elseif(strtolower($format) == "full")
{
$dformat = "l F d, Y";
$tformat = "H:i:s a e";
}
if(isset($dformat) && isset($tformat))
{
$date = date($dformat,mktime($cdbit[0],$cdbit[1],$cdbit[2],$bdbit[1],$bdbit[2],$bdbit[0])) . " " . date($tformat,mktime($cdbit[0],$cdbit[1],$cdbit[2],$bdbit[1],$bdbit[2],$bdbit[0]));
}
else
{
$date = date($format,mktime($cdbit[0],$cdbit[1],$cdbit[2],$bdbit[1],$bdbit[2],$bdbit[0]));
}
return $date;
}
?>
Usage:
<?php
$date = "2007-05-19 13:51:22";
// You can use either
echo DateFormat($date,"F d, Y, g:i a");
// Or you can use
echo DateFormat($date,"long");
?>
The resulting output is: May 19, 2007 1:51 pm