Foros del Web » Programando para Internet » PHP »

funcion date_diff() modificar

Estas en el tema de funcion date_diff() modificar en el foro de PHP en Foros del Web. que tal a todos muy buen dia, quisiera saber si alguien me puede ayudar a modificar esta funcion para que me acepte otro tipo de ...
  #1 (permalink)  
Antiguo 11/07/2009, 09:23
 
Fecha de Ingreso: marzo-2009
Mensajes: 163
Antigüedad: 15 años
Puntos: 0
funcion date_diff() modificar

que tal a todos muy buen dia, quisiera saber si alguien me puede ayudar a modificar esta funcion para que me acepte otro tipo de formato de fecha, porque el que este acepta es '1 July 2003' y quiero que acepte '2003-05-01' o de que manera puedo convertir ese formato que acepta al que quiero, saludos



Código PHP:
<?php
     
    
function datediff($interval$datefrom$dateto$using_timestamps false) {
    
/*
    $interval can be:
    yyyy - Number of full years
    q - Number of full quarters
    m - Number of full months
    y - Difference between day numbers
   (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
   d - Number of full days
   w - Number of full weekdays
   ww - Number of full weeks
   h - Number of full hours
   n - Number of full minutes
   s - Number of full seconds (default)
   */
  
   
if (!$using_timestamps) {
   
$datefrom strtotime($datefrom0);
   
$dateto strtotime($dateto0);
   }
   
$difference $dateto $datefrom// Difference in seconds
  
   
switch($interval) {
  
   case 
'yyyy'// Number of full years
    
   
$years_difference floor($difference 31536000);
   if (
mktime(date("H"$datefrom), date("i"$datefrom), date("s"$datefrom), date("n"$datefrom), date("j"$datefrom), date("Y"$datefrom)+$years_difference) > $dateto) {
   
$years_difference--;
   }
   if (
mktime(date("H"$dateto), date("i"$dateto), date("s"$dateto), date("n"$dateto), date("j"$dateto), date("Y"$dateto)-($years_difference+1)) > $datefrom) {
   
$years_difference++;
   }
   
$datediff $years_difference;
   break;
    
   case 
"q"// Number of full quarters
    
   
$quarters_difference floor($difference 8035200);
   while (
mktime(date("H"$datefrom), date("i"$datefrom), date("s"$datefrom), date("n"$datefrom)+($quarters_difference*3), date("j"$dateto), date("Y"$datefrom)) < $dateto) {
   
$months_difference++;
   }
   
$quarters_difference--;
   
$datediff $quarters_difference;
   break;
    
   case 
"m"// Number of full months
    
   
$months_difference floor($difference 2678400);
   while (
mktime(date("H"$datefrom), date("i"$datefrom), date("s"$datefrom), date("n"$datefrom)+($months_difference), date("j"$dateto), date("Y"$datefrom)) < $dateto) {
   
$months_difference++;
   }
   
$months_difference--;
   
$datediff $months_difference;
   break;
    
   case 
'y'// Difference between day numbers
    
   
$datediff date("z"$dateto) - date("z"$datefrom);
   break;
    
   case 
"d"// Number of full days
    
  
$datediff floor($difference 86400);
   break;
    
   case 
"w"// Number of full weekdays
    
   
$days_difference floor($difference 86400);
   
$weeks_difference floor($days_difference 7); // Complete weeks
   
$first_day date("w"$datefrom);
   
$days_remainder floor($days_difference 7);
   
$odd_days $first_day $days_remainder// Do we have a Saturday or Sunday in the remainder?
   
if ($odd_days 7) { // Sunday
   
$days_remainder--;
   }
   if (
$odd_days 6) { // Saturday
   
$days_remainder--;
   }
   
$datediff = ($weeks_difference 5) + $days_remainder;
   break;
    
   case 
"ww"// Number of full weeks
    
   
$datediff floor($difference 604800);
   break;
    
   case 
"h"// Number of full hours
    
   
$datediff floor($difference 3600);
   break;
    
   case 
"n"// Number of full minutes
    
   
$datediff floor($difference 60);
   break;
    
  default: 
// Number of full seconds (default)
   
  
$datediff $difference;
  break;
  }
 
  return 
$datediff;
   
  }
   
  
?>
  #2 (permalink)  
Antiguo 11/07/2009, 09:43
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: funcion date_diff() modificar

¿Lo trataste a ver si te funciona como pides? Porque yo lo probe asi como tu lo pides y funciono. Lo que pasa es que dentro de la funcion tiene declarado strtotime() y eso lo que hace es tratar de cambiar el string en fecha. Por lo tanto lo que estas pidiendo tambien lo va a reconocer
  #3 (permalink)  
Antiguo 11/07/2009, 09:54
 
Fecha de Ingreso: marzo-2009
Mensajes: 163
Antigüedad: 15 años
Puntos: 0
Respuesta: funcion date_diff() modificar

podrias pegar el como lo calaste porfavor? es que no me sale...... el ejemplo que yo tengo del otro formato es asi...echo datediff('w', '1 July 2003', '2 July 2003', false);
  #4 (permalink)  
Antiguo 11/07/2009, 09:56
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: funcion date_diff() modificar

pues como tu lo pides

datediff('w', '2009-07-01', '2010-07-01', false);

Acuerdate que el segundo parametro es la fecha inicial y el tercer parametro es la fecha final
  #5 (permalink)  
Antiguo 11/07/2009, 10:14
 
Fecha de Ingreso: marzo-2009
Mensajes: 163
Antigüedad: 15 años
Puntos: 0
listo ya salio, muchas gracias machin

oye esprame jajajaja ahora no me esta jalando ahora extraigo mis variables de sql y no jala....tengo este renglon

echo datediff('w', '$datos[\'const_date_emi\']', '$datos[\'const_date_calc\']', false);

Última edición por GatorV; 11/07/2009 a las 14:31
  #6 (permalink)  
Antiguo 11/07/2009, 11:05
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: funcion date_diff() modificar

porque no debes colocarlo entre comillas

Código PHP:
echo datediff('w'$datos['const_date_emi'], $datos['const_date_calc'], false); 
  #7 (permalink)  
Antiguo 11/07/2009, 11:16
 
Fecha de Ingreso: marzo-2009
Mensajes: 163
Antigüedad: 15 años
Puntos: 0
Respuesta: funcion date_diff() modificar

pregunta...porque si pongo ejemplo echo datediff ('w', '2009-07-20' , '2009-07-19', false) me arroja de resultado -6?
  #8 (permalink)  
Antiguo 11/07/2009, 11:18
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: funcion date_diff() modificar

Porque fecha inicio no puede ser mayor a fecha final

acuerdate que el segundo parametro es la fecha inicio y el tercer parametro es fecha final
  #9 (permalink)  
Antiguo 11/07/2009, 11:24
 
Fecha de Ingreso: marzo-2009
Mensajes: 163
Antigüedad: 15 años
Puntos: 0
Respuesta: funcion date_diff() modificar

pero ya hablando en dias...porque sale -6.........si seria el resultado debe de ser -1 no crees?
  #10 (permalink)  
Antiguo 11/07/2009, 11:36
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: funcion date_diff() modificar

Porque estas usando el primer parametro como w que dice Number of full weekdays. Tu debes usar el de d

Me dejas saber
  #11 (permalink)  
Antiguo 11/07/2009, 11:47
 
Fecha de Ingreso: marzo-2009
Mensajes: 163
Antigüedad: 15 años
Puntos: 0
ohhhhhhhhhhhhh que guey soy ajjajajajajaja gracias vato te debo una

oye una ultima pregunta....con el w..agarra los dias laborales? o eso no esta en la funcion de php_diff....dias laborales de lunes a viernes

Última edición por GatorV; 11/07/2009 a las 14:31
  #12 (permalink)  
Antiguo 11/07/2009, 11:53
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 10 meses
Puntos: 1517
Respuesta: funcion date_diff() modificar

Leyendo lo que hace con el w vas a ver que la persona que hizo este script elimina domingos y sabados

Código PHP:
&#37;
   
case "w"// Number of full weekdays 
   
$days_difference floor($difference 86400); 
   
$weeks_difference floor($days_difference 7); // Complete weeks 
   
$first_day date("w"$datefrom); 
   
$days_remainder floor($days_difference 7); 
   
$odd_days $first_day $days_remainder// Do we have a Saturday or Sunday in the remainder? 
   
if ($odd_days 7) { // Sunday 
   
$days_remainder--; 
   } 
   if (
$odd_days 6) { // Saturday 
   
$days_remainder--; 
   } 
   
$datediff = ($weeks_difference 5) + $days_remainder
   break; 
Por lo tanto solo lee de lunes a viernes. Te sugiero que leas los cases y lo que el hace para entiendas mejor que hace en cada letra.
  #13 (permalink)  
Antiguo 11/07/2009, 12:38
 
Fecha de Ingreso: marzo-2009
Mensajes: 163
Antigüedad: 15 años
Puntos: 0
Respuesta: funcion date_diff() modificar

worale nice....gracias machin..me ayudaste mucho
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 16:48.