Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   PHP (http://www.forosdelweb.com/f18/)
-   -   cual fecha es mayor? (http://www.forosdelweb.com/f18/cual-fecha-mayor-594117/)

farra 06/06/2008 10:34

cual fecha es mayor?
 
necesito calcular cual fecha es mayor...

tengo esta funcion.. pero no me funciona...

Código PHP:


function cualfechaesmayor($fecha1$fecha2){
// Formato dd/mm/YYYY H:i:s
if($fecha1 $fecha2){
$result="1";
}
if(
$fecha1 $fecha2){
$result="2";
}
if(
$fecha1 $fecha2){
$result="0";
}

// devuelve  0 si son iguales, 1 si es mayor la fecha1 y 2 si es mayor la fecha2
return $result;


como puedo hacerlo? help!

pateketrueke 06/06/2008 10:46

Respuesta: cual fecha es mayor?
 
necesitas construir las fechas a un formato de entero, y no string... como lo haces

puedes aliarte de mktime() ... así que, suerte!

farra 06/06/2008 11:08

Respuesta: cual fecha es mayor?
 
mmmm... y como hago eso? si me pones el codigo voy a entender un mejor...

pateketrueke 06/06/2008 11:23

Respuesta: cual fecha es mayor?
 
claro... aquí esta!

http://php.net/mktime (en la pagina de PHP hay muchos ejemplos)

farra 06/06/2008 11:31

Respuesta: cual fecha es mayor?
 
aca esta el codigo que hice con el MKTIME:


Código PHP:


function cualesmayor($fecha1,$fecha2){
// Formato de entrada d/m/Y H:i:s

// separa hora y fecha
//1
$fecha1 explode(" ",$fecha1);
$fec1=$fecha1[0];
$hor1=$fecha1[1];
//2
$fecha2 explode(" ",$fecha2);
$fec2=$fecha2[0];
$hor2=$fecha2[1];

// separar fecha
//1
$fech1=explode("/",$fec1);
$dia1=intval($fech1[0]);
$mes1=intval($fech1[1]);
$ano1=intval($fech1[2]);
//2
$fech2=explode("/",$fec2);
$dia2=intval($fech2[0]);
$mes2=intval($fech2[1]);
$ano2=intval($fech2[2]);

// separar hora
//1
$hors1=explode(":",$hor2);
$hora1=intval($hors1[0]);
$minut1=intval($hors1[1]);
$segun1=intval($hors1[2]);
//2
$hors2=explode(":",$hor2);
$hora2=intval($hors2[0]);
$minut2=intval($hors2[1]);
$segun2=intval($hors2[2]);


// convertimos a timestamp
$fechatime1=mktime ($hora1,$minut1,$segun1,$mes1,$dia1,$ano1);
$fechatime2=mktime ($hora2,$minut2,$segun2,$mes2,$dia2,$ano2);

// comparamos cual es mayor, cual menor o si son iguales
if($fechatime1 $fechatime2){
$result="0";
}
if(
$fechatime1 $fechatime2){
$result="1";
}
if(
$fechatime1 $fechatime2){
$result="2";
}

// retornamos el resultado

return $result;



pero no me funciona, siempre me devuelve cero...

donde esta el error?

guille_el3 06/06/2008 11:33

Respuesta: cual fecha es mayor?
 
Otra posibilidad, es si ambas fechas son strings hacer algo como:

Código PHP:

function fechaMayor($fecha1$fecha2){
return 
strtotime($fecha1) > strtotime($fecha2);



strtotime\1 recibe un string fecha y retorna un numero entero equivalente a esa fecha. Si estas usando una de las últimas versiones de php, que admiten booleans, entonces esa función estará bien así. Sino, sin booleans la version sería:

Código PHP:

function fechaMayor($fecha1$fecha2){
return 
strtotime($fecha1) - strtotime($fecha2);



Si es mayor, el valor de retorno sera positivo, si son iguales sera 0 y si es menor retornará negativo.
Suerte!

maikel76 06/06/2008 11:33

Respuesta: cual fecha es mayor?
 
Buenas, necesito una ayuda. Tengo un reporte que entregar (DataReport) y ya hice una sentencia en Sql y esoty filtrando la fecha muy bien y ahora necesito filtrar la fecha mas otro campo, osea, queiro que me refleje si el Contribuyente CANTV(ejemplo) ha pagado en el mes de mayo del 2008. entonces ya yengo filtrada la fecha y se lo muestro como filtro tambien el campo + la fecha.

Private Sub cmbconsulta_click()
Dim MiSQL As String
On Error GoTo Cancel
Cancel:
If Err.Number = 13 Then
Close
Exit Sub
End If
Dim respt
x = Date
If DtpDesde > x Then
MsgBox "La Fecha Inicial no Puede Ser Mayor a la Fecha Actual", vbExclamation, "Consultas"
Exit Sub
End If
If DtpHasta > x Then
MsgBox "La Fecha Final no Puede Ser Mayor a la Fecha Actual", vbExclamation, "Consultas"
Exit Sub
End If
MiSQL = "SELECT * FROM Certificación WHERE Emision_factura Between #" & Format(DtpDesde, "m/d/yyyy") & "# And #" & Format(DtpHasta + 1, "m/d/yyyy") & "# order by Nombre" Debug.Print MiSQL
If adoImpDia.State = adStateOpen Then adoImpDia.Close
Enlace adoImpDia, MiSQL
Set RptGeneral.DataSource = adoImpDia
RptGeneral.Title = "Reporte General del Sistema" & " Desde " & DtpDesde & " Hasta " & DtpHasta
RptGeneral.Caption = "Reporte General del Sistema"
RptGeneral.Show
End Sub
este es mi filtro actual pero quiero agregarle a la sentencia Sql la fecha mas el campo.

guille_el3 06/06/2008 11:37

Respuesta: cual fecha es mayor?
 
Cita:

Iniciado por maikel76 (Mensaje 2438197)
Buenas, necesito una ayuda. Tengo un reporte que entregar (DataReport) y ya hice una sentencia en Sql y esoty filtrando la fecha muy bien y ahora necesito filtrar la fecha mas otro campo, osea, queiro que me refleje si el Contribuyente CANTV(ejemplo) ha pagado en el mes de mayo del 2008. entonces ya yengo filtrada la fecha y se lo muestro como filtro tambien el campo + la fecha.

Private Sub cmbconsulta_click()
Dim MiSQL As String
On Error GoTo Cancel
Cancel:
If Err.Number = 13 Then
Close
Exit Sub
End If
Dim respt
x = Date
If DtpDesde > x Then
MsgBox "La Fecha Inicial no Puede Ser Mayor a la Fecha Actual", vbExclamation, "Consultas"
Exit Sub
End If
If DtpHasta > x Then
MsgBox "La Fecha Final no Puede Ser Mayor a la Fecha Actual", vbExclamation, "Consultas"
Exit Sub
End If
MiSQL = "SELECT * FROM Certificación WHERE Emision_factura Between #" & Format(DtpDesde, "m/d/yyyy") & "# And #" & Format(DtpHasta + 1, "m/d/yyyy") & "# order by Nombre" Debug.Print MiSQL
If adoImpDia.State = adStateOpen Then adoImpDia.Close
Enlace adoImpDia, MiSQL
Set RptGeneral.DataSource = adoImpDia
RptGeneral.Title = "Reporte General del Sistema" & " Desde " & DtpDesde & " Hasta " & DtpHasta
RptGeneral.Caption = "Reporte General del Sistema"
RptGeneral.Show
End Sub
este es mi filtro actual pero quiero agregarle a la sentencia Sql la fecha mas el campo.

No se si esta bueno que uses threads de otro para hacer tus consultas, y menos cuando no tienen mucho que ver... Igual te respondo, cualquier cosa que lo muevan:

SELECT campos
FROM tabla1,tabla2,...,tablan
WHERE (condicion1) AND (condicion2) AND (condicion3) OR(condicion4)
ORDER BY campo

espero que te sirva :)

farra 06/06/2008 11:52

Respuesta: cual fecha es mayor?
 
cambie la funcion con el strotime.. y sigue sin funcionar...

no entiendo por que me da igual el valor siempre... si las fechas son distintas...


Código PHP:


function cualesmayor($fecha1,$fecha2){
// Formato de entrada d/m/Y H:i:s

// separa hora y fecha
//1
$fecha1 explode(" ",$fecha1);
$fec1=$fecha1[0];
$hor1=$fecha1[1];
//2
$fecha2 explode(" ",$fecha2);
$fec2=$fecha2[0];
$hor2=$fecha2[1];

// separar fecha
//1
$fech1=explode("/",$fec1);
$dia1=$fech1[0];
$mes1=$fech1[1];
$ano1=$fech1[2];
//2
$fech2=explode("/",$fec2);
$dia2=$fech2[0];
$mes2=$fech2[1];
$ano2=$fech2[2];

// separar hora
//1
$hors1=explode(":",$hor1);
$hora1=$hors1[0];
$minut1=$hors1[1];
$segun1=$hors1[2];
//2
$hors2=explode(":",$hor2);
$hora2=$hors2[0];
$minut2=$hors2[1];
$segun2=$hors2[2];

/*
// convertimos a timestamp
$fechatime1=mktime ($hora1,$minut1,$segun1,$mes1,$dia1,$ano1);
$fechatime2=mktime ($hora2,$minut2,$segun2,$mes2,$dia2,$ano2);
*/


$fechamy1=$ano1."-".$mes1."-".$dia1." ".$hora1.":".$minut1.":".$segun1;
$fechamy2=$ano2."-".$mes2."-".$dia2." ".$hora2.":".$minut2.":".$segun2;
$fechatime1=strtotime($fechamy1);
$fechatime2=strtotime($fechamy2);

// comparamos cual es mayor, cual menor o si son iguales
if($fechatime1 $fechatime2){
$result="0";
}
if(
$fechatime1 $fechatime2){
$result="1";
}
if(
$fechatime1 $fechatime2){
$result="2";
}

// imprimimos el resultado
// devuelve  0 si son iguales, 1 si es mayor la fecha1 y 2 si es mayor la fecha2
//$result=$fechamy1."<br />".$fechamy2;
return $result;



asi le llamo:

Código PHP:

<?php $mayor=cualesmayor("02/04/2008 12:33:05","03/04/2008 12:33:06");
echo 
$mayor;
 
?>


farra 06/06/2008 11:55

Respuesta: cual fecha es mayor?
 
bue.. al fin le encontre el error.. y era un error estupido...!

Error:
if($fechatime1 = $fechatime2){
$result="0";
}

Coreccion:
if($fechatime1 == $fechatime2){
$result="0";
}

me trague un '='


La zona horaria es GMT -6. Ahora son las 01:56.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.