date calculations
| From: | Emilio | Date: | Thu, 24 Jul 2003 09:00:46 +0000 |
| Subject: | date calculations | ||
| Groups: | php.windows | ||
| Request: | Send a blank email to php-windows+get-20895@lists.php.net to get a copy of this message | ||
Dear Sirs,
I wrote a class for date calculations. You can read it below as well as an example. Attached also.
I recall someone asked a few days ago about 20 day forward date. This class provides a solution for
it.
I submitted it also to phpclasses.com script library.
Please, feel free to use it and change it as well.
Best Regards,
------------------------------------------------------------------------------
<?php
/*-------------------------------------------*
| script: cdate.php |
| code of class CDate for |
| date manipulation |
| entry data: year, month and day |
| (in absence: current date is default) |
| exit through method ReadDate() who |
| returns date in format date("Y-m-d") |
| |
| Author: Emilio Antonio Rodríguez Padrón |
| e-mail: emilioarp@ifrance.com |
| |
| date: July 22, 2003 |
*--------------------------------------------*/
class CDate
{
var $mDate; // in actual OOP it should be private
function CDate($y=0,$m=0,$d=0)
{
If ($y==0) // these three "if" are necessary because default parameters must be
constants.
{ $y=Date("Y"); }
If ($m==0)
{ $m=Date("m"); }
if ($d==0)
{ $d=Date("d"); }
$this -> mDate = Date("Y-m-d",mktime(0,0,0,$m,$d,$y));
} // end of constructor
function AddTime($y=0,$m=0,$d=0) // this method adds y years, m months and d days to date
{
$array_date = explode("-",$this->mDate);
$this->mDate=Date("Y-m-d",mktime(0,0,0,$array_date[1]+$m,$array_date[2]+$d,$array_date[0]+$y));
} // end of AddTime
// If you are a rigorous man, the following method should be your way to access date
function ReadDate()
{
return $this->mDate; // you get the date in same format as Date("Y-m-d")
} // ends of ReadDate
} // end of declaration class CDate
?>
--------------------------------------------------------------------------------------------------------------------------------------------------
<?php
// script example.php
// uses class CDate
// for date of tomorrow calculation
include("cdate.php");
$tomorrow = new CDate();
$tomorrow -> AddTime(0,0,1);
echo "Date of tomorrow is: ".$tomorrow->ReadDate();
echo "<br /><br />";
$OneMonthAfterJuly4 = new CDate(2003,7,4);
$OneMonthAfterJuly4 -> AddTime(0,1,0);
echo "Date of one month after July 4, 2003 is: ".$OneMonthAfterJuly4 -> ReadDate();
?>