clase para trabajar fechas

From: Date: Wed, 23 Jul 2003 08:37:21 +0000
Subject: clase para trabajar fechas
Groups: php.general.es 
Request: Send a blank email to general-es+get-38715@lists.php.net to get a copy of this message
Hola, Escribí una clase para trabajar con las fechas. Me gustaría compartirla con vosotros y espero a alguno pueda serle útil. La publico bajo las condiciones acostumbradas OpenSource, en fin quien lo desee puede modificarla a voluntad, completa o parcialmente. Yo no sé si esta lista es el marco apropiado, me disculpo si no es el caso. También la voy a cargar chez Nexen (www.nexen.net) (es un sitio en Francés con una biblioteca de scripts php). Por razones diversas, he puesto los nombres de variables y comentarios en Inglés, o al menos lo he intentado. Abajo pongo el código de la clase y un ejemplo. También los pongo como datos adjuntos a este mensaje. saludos ------------------------------------------------------------------------------- <?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(); ?>

Attachment: [application/octet-stream] example.php
Attachment: [application/octet-stream] cdate.php
« previous php.general.es (#38715) next »