This class is to designed date function. The idea behind to create this class is to create a template which enable user to perform action in user. There are different function included in this class for different purpose. The user can change the format of date, add days to current date, add month to current date, add year to current date, subtract number of days from current date, subtract number of months from current date, subtract number of year from current date, get the difference between new date and current date.
/*
* Created on Apr 2, 2010
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
abstract class My_date
{
private $_date;
/*
*Return the current date and time of the sever
*/
static function getCurrentDate()
{
$cur_date = date('D, M d, Y, g::i a');
return $cur_date;
}
/*
* change the format of the date
* input: date and num
* output : formated date
*/
static function ChangeDateFormat($num,$date)
{
$_date = strtotime($date);
switch ($num)
{
case 2:
$_date = date('Y-m-d',$_date);
break;
case 3:
$_date = date('Y/m/d',$_date);
break;
case 4:
$_date= date('Y.m.d',$_date);
break;
case 5:
$_date = date('y-m-d',$_date);
break;
case 6:
$_date = date('y/m/d',$_date);
break;
case 7:
$_date= date('y.m.d',$_date);
break;
case 8:
$_date = date('d-m-Y',$_date);
break;
case 9:
$_date = date('d/m/Y',$_date);
break;
case 10:
$_date= date('d.m.Y',$_date);
break;
case 11:
$_date= date('d M Y',$_date);
break;
case 12:
$_date= date('d M, Y',$_date);
break;
case 13:
$_date= date('M d, Y',$_date);
break;
case 14:
$_date= date('D d M Y',$_date);
break;
case 15:
$_date= date('D, d M, Y',$_date);
break;
case 16:
$_date= date('D, M d, Y',$_date);
break;
}
return $_date;
}
/*
* Add the number of days to the current date
* input: date and number of days
* output: return the date with added days.
*/
static function AddDays($date,$days)
{
$number = $days;
$_date=strtotime($date);
$_add = $_date + $days*24*60*60;
$_date = date('d-m-Y',$_add);
return $_date;
}
/*
* Add the number of moths to the date
* input:number of months and date
* output: return the new date with added number of month
*/
static function AddMonths($date,$month)
{
$_date=strtotime($date);
$_month = date("m",$_date);
$_d= date("d",$_date);
$_y=date("Y",$_date);
$_m =$_month +$month;
if($_m>12)
{
$_m=$_m-12;
}
if($_m<$_month)
{
$_y++;
}
$count =cal_days_in_month ( CAL_GREGORIAN, $_m, $_y);
if($count$_d2)
{
$_d = $_d1-$_d2;
}
else
{
$_d=$_d2-$_d1;
}
if($_m1>$_m2)
{
$_m = $_m1-$_m2;
}
else
{
$_m=$_m2-$_m1;
}
if($_y1>$_y2)
{
$_y = $_y1-$_y2;
}
else
{
$_y=$_y2-$_y1;
}
echo "Difference between two date
";
echo "days ".$_d;
echo " month ".$_m;
echo " year ".$_y;
}
}