Ver Mensaje Individual
  #5 (permalink)  
Antiguo 21/07/2011, 11:13
Avatar de DinamiteDog
DinamiteDog
 
Fecha de Ingreso: febrero-2005
Ubicación: Rosario, Argentina
Mensajes: 77
Antigüedad: 19 años, 2 meses
Puntos: 4
Respuesta: Consulta PHP funcion date()

Aquí tienes una clase que funciona de maravillas para operaciones con fechas:

Código PHP:
Ver original
  1. <?
  2. /*********************************************************************************
  3. - Date operations by André Cupini
  4. - Sum or subtract day, months or years from any date
  5. - Ex:
  6. require_once("class_dt.php");
  7. $dt = new DT;
  8. $date = $dt->operations("06/01/2003", "sum", "day", "4")   // Return 10/01/2003
  9. $date = $dt->operations("06/01/2003", "sub", "day", "4")   // Return 02/01/2003
  10. $date = $dt->operations("06/01/2003", "sum", "month", "4") // Return 10/05/2003
  11. *********************************************************************************/
  12. class DT
  13. {
  14.     // Função que soma ou subtrai, dias, meses ou anos de uma data qualquer
  15.     function operations($date, $operation, $where = FALSE, $quant, $return_format = FALSE)
  16.     {
  17.         // Verifica erros
  18.         $warning = "<br>Warning! Date Operations Fail... ";
  19.         if(!$date || !$operation) {
  20.             return "$warning invalid or inexistent arguments<br>";
  21.         }else{
  22.             if(!($operation == "sub" || $operation == "-" || $operation == "sum" || $operation == "+")) return "<br>$warning Invalid Operation...<br>";
  23.             else {
  24.                 // Separa dia, mês e ano
  25.                 list($day, $month, $year) = split("/", $date);
  26.  
  27.                 // Determina a operação (Soma ou Subtração)
  28.                 ($operation == "sub" || $operation == "-") ? $op = "-" : $op = '';
  29.  
  30.                 // Determina aonde será efetuada a operação (dia, mês, ano)
  31.                 if($where == "day")   $sum_day     = $op."$quant";
  32.                 if($where == "month") $sum_month = $op."$quant";
  33.                 if($where == "year")  $sum_year     = $op."$quant";
  34.                  
  35.                 // Gera o timestamp
  36.                 $date = mktime(0, 0, 0, $month + $sum_month, $day + $sum_day, $year + $sum_year);
  37.                  
  38.                 // Retorna o timestamp ou extended
  39.                 ($return_format == "timestamp" || $return_format == "ts") ? $date = $date : $date = date("d/m/Y", "$date");
  40.  
  41.                 // Retorna a data
  42.                 return $date;
  43.             }
  44.         }
  45.     }
  46. }
  47. ?>
http://www.phpclasses.org/browse/file/3126.html

Saludos!
__________________
All generalizations are false, including this one ~ Mark Twain