Foros del Web » Programando para Internet » PHP »

Manejo de fechas con ComboBox

Estas en el tema de Manejo de fechas con ComboBox en el foro de PHP en Foros del Web. Hola a todos.. Tengo el siguiente problemita... quiero manejar fechas en un comboBox dentro de un formulario. Tengo una fecha de alquiler desde y la ...
  #1 (permalink)  
Antiguo 22/11/2005, 09:22
 
Fecha de Ingreso: octubre-2005
Mensajes: 69
Antigüedad: 18 años, 4 meses
Puntos: 0
Exclamación Manejo de fechas con ComboBox

Hola a todos.. Tengo el siguiente problemita... quiero manejar fechas en un comboBox dentro de un formulario. Tengo una fecha de alquiler desde y la otra hasta... ya llene los comboBox, pero cuando deseo seleccionar un mes que tiene 30 ó 28 dias el comboBox del día no se actualiza.. aqui le envio el codigo que tengo hasta los momentos...

Código PHP:
<html> 
<head> 
</head> 
<body>
<form name=CMSFORM method=post>
<table width=100% border=1 align=center>

<?php 
$day 
date("d");
$moth date("m");
$yeardate("Y");
$cont=0;
$cont_day=1;
$stop=32;

$array=array("Enero""Febrero""Marzo""Abril""Mayo""Junio""Julio""Agosto""Septiembre""Octubre""Noviembre""Diciembre");

?>

<!-- **** FECHA ***** -->
    <tr> 
    <td align=right><b>Fecha de alquiler:</b></td>
    <td>
         <table border=1>
          <tr>
               <td align="center" width=100><b>Desde: </b></td> 
            <td align="right"> Año: </td> 
            <td width=100> 
        <select name="a_o">
           <option value="A"><?php echo $year?></option>
           <option value="B"><?php echo $year+1?></option>
        </select> 
            </td> 
            <td align="right"> Mes: </td> 
            <td width=100> 
        <select name="mes">                
           <?php 
            
while ($cont 12) { ?>
                <option value=<?php echo $cont" "; if(($cont+1) == $moth){echo ("selected");}?>>
                                                               <?php echo $array[$cont]; 
                                                                
$cont++; }?>
            </option>                                 
        </select> 
            </td>
            <td align="right"> Día:    </td> 
            <td width=100> 
        <select name="dia">
        <?php 
           
if ($moth == 04 || $moth ==06 || $moth ==09 || $moth ==11) {
              
$stop=31;
        } 
        if (
$moth == 02) {
           
$stop=29;
        }
        while (
$cont_day $stop) { ?>
             <option value=<?php echo $cont_day" "; if(($cont_day) == $day){echo ("selected");}?>>
                                                        <?php echo $cont_day
                                                            
$cont_day++; }?>
              </option>
        </select> 
              </td> 
           </tr>
        </table>
    </td>
        </tr> 

<!-- **** FECHA ***** -->
<?php 
    $cont
=0;
    
$cont_day=1;
    
$stop=32;
?>
    <tr> 
        <td></td>
        <td>
        <table border=1>
        <tr>
            <td align="center" width=100><b>Hasta: </b></td> 
            <td align="right"> Año: </td> 
            <td width=100> 
              <select name="a_o2">
                <option value="A2"><?php echo $year?></option>
                <option value="B2"><?php echo $year+1?></option>
              </select> 
            </td> 
            <td align="right"> Mes: </td> 
            <td width=100> 
                <select name="mes2">                
                    <?php 
                        
while ($cont 12) { ?>
                            <option value="2"<?php echo $cont" "; if(($cont+1) == $moth){echo ("selected");}?>>
                                <?php echo $array[$cont]; 
                                      
$cont++; }?>
                            </option>                                 
                </select> 
            </td>
            <td align="right"> Día:    </td> 
            <td width=100>                 
                <select name="dia2">
                    <?php 
                        
if ($moth == 04 || $moth ==06 || $moth ==09 || $moth ==11) {
                            
$stop=31;
                        } 
                        if (
$moth == 02) {
                            
$stop=29;
                        }
                        while (
$cont_day $stop) { ?>
                            <option value=<?php echo $cont_day" "; if(($cont_day) == $day){echo ("selected");}?>>
                                <?php echo $cont_day
                                      
$cont_day++; }?>
                        </option>
                </select> 
            </td> 
        </tr>
        </table>
        </td>
    </tr>
<!-- **** FECHA ***** -->
</table>
</body> 
</html>
Gracias...
__________________
Ana Martins
  #2 (permalink)  
Antiguo 22/11/2005, 09:32
Avatar de Seppo  
Fecha de Ingreso: marzo-2005
Ubicación: Buenos Aires, Argentina
Mensajes: 1.284
Antigüedad: 19 años
Puntos: 17
El problema es que PHP se ejecuta una vez y al inicio. Hace poco hice una mezcla de PHP con JS para algo parecido, creo que te puede servir.
El código de JS lo saqué de las FAQS de aquí y lo adapté un poco :)

Código PHP:
<?php
if ($_POST['action'] == "buscar") {
#código
}

$fecha['dia'] = date("d");
$fecha['mes'] = date("m");
$fecha['ano'] = date("Y");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  <script language="JavaScript">
   function hab(act) {
    if (act == "D") {
      document.informe.diadesde.disabled = !document.informe.desde.checked;
      document.informe.mesdesde.disabled = !document.informe.desde.checked;
      document.informe.anodesde.disabled = !document.informe.desde.checked;
    } else {
      document.informe.diahasta.disabled = !document.informe.hasta.checked;
      document.informe.meshasta.disabled = !document.informe.hasta.checked;
      document.informe.anohasta.disabled = !document.informe.hasta.checked;
    }
   }
   function addOpt(oCntrl, iPos, sTxt, sVal){
     var selOpcion=new Option(sTxt, sVal);
     eval(oCntrl.options[iPos]=selOpcion);
   }

   function cambia(oCntrl, from) {
    var a = 30;
    var cuantosdias = 27;
    var valoractual = oCntrl.value;
    if (from.selectedIndex == 0 || from.selectedIndex == 2 || from.selectedIndex ==  4 || from.selectedIndex == 6 || from.selectedIndex == 7 || from.selectedIndex == 9 ||  from.selectedIndex == 11) {
        cuantosdias+=3;
        addOpt(oCntrl,  28, "29", "29");
        addOpt(oCntrl,  29, "30", "30");
        addOpt(oCntrl,  30, "31", "31");
    } else {
        if (from.selectedIndex == 3 || from.selectedIndex == 5 ||  from.selectedIndex == 8 || from.selectedIndex == 10) {
            cuantosdias+=2;
            addOpt(oCntrl,  28, "29", "29");
            addOpt(oCntrl,  29, "30", "30");
        } else {
            if (from == document.informe.mesdesde) {
                if (document.informe.anodesde.value / 4 == Math.floor(document.informe.anodesde.value / 4)) {
                    cuantosdias+=1;
                    addOpt(oCntrl,  28, "29", "29");
                }
            } else {
                if (document.informe.anohasta.value / 4 == Math.floor(document.informe.anohasta.value / 4)) {
                    cuantosdias+=1;
                    addOpt(oCntrl,  28, "29", "29");
                }
            }
        }
    }
    while(cuantosdias < a) {
         oCntrl.options[a]=null;
         a -= 1;
     }
     
    oCntrl.value = valoractual;
    return false;
  }
   function esDigito(sChr){
    var sCod = sChr.charCodeAt(0);
    return ((sCod > 47) && (sCod < 58));
   }

   function valSep(oTxt){
    var bOk = false;
    var sep1 = oTxt.charAt(2);
    var sep2 = oTxt.charAt(5);
    bOk = bOk || ((sep1 == "-") && (sep2 == "-"));
    bOk = bOk || ((sep1 == "/") && (sep2 == "/"));
    return bOk;
   }

   function finMes(oTxt){
    var nMes = parseInt(oTxt.substr(3, 2), 10);
    var nAno = parseInt(oTxt.substr(6), 10);
    var nRes = 0;
    switch (nMes){
     case 1: nRes = 31; break;
     case 2: 
     if (Math.floor(nAno/4) == nAno/4) {
        nRes = 29; break;
     } else {
        nRes = 28; break;
     }
     case 3: nRes = 31; break;
     case 4: nRes = 30; break;
     case 5: nRes = 31; break;
     case 6: nRes = 30; break;
     case 7: nRes = 31; break;
     case 8: nRes = 31; break;
     case 9: nRes = 30; break;
     case 10: nRes = 31; break;
     case 11: nRes = 30; break;
     case 12: nRes = 31; break;
    }
    return nRes + (((nMes == 2) && (nAno % 4) == 0)? 1: 0);
   }

   function valDia(oTxt){
    var bOk = false;
    var nDia = parseInt(oTxt.substr(0, 2), 10);
    bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
    return bOk;
   }

   function valMes(oTxt){
    var bOk = false;
    var nMes = parseInt(oTxt.substr(3, 2), 10);
    bOk = bOk || ((nMes >= 1) && (nMes <= 12));
    return bOk;
   }

   function valAno(oTxt){
    var bOk = true;
    var nAno = oTxt.substr(6);
    bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));
    if (bOk){
     for (var i = 0; i < nAno.length; i++){
      bOk = bOk && esDigito(nAno.charAt(i));
     }
    }
    return bOk;
   }
    
    function valFecha(oTxt, oTxt2){
        var bOk = true;
        var bOk2 = true;
        if (oTxt != ""){
            bOk = bOk && (valAno(oTxt));
            bOk = bOk && (valMes(oTxt));
            bOk = bOk && (valDia(oTxt));
            bOk = bOk && (valSep(oTxt));
            if (!informe.desde.checked) {
                bOk = true;
            }
            if (!bOk){
                alert("Fecha inicial inválida");
            } else {
                bOk2 = bOk2 && (valAno(oTxt2));
                bOk2 = bOk2 && (valMes(oTxt2));
                bOk2 = bOk2 && (valDia(oTxt2));
                bOk2 = bOk2 && (valSep(oTxt2));
                if (!informe.hasta.checked) {
                    bOk2 = true;
                }
                if (!bOk2){
                    alert("Fecha final inválida");
                } else {
                    document.informe.submit(); 
                }
            }
        }
    }
  </script> 
<title>neonet</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../css/main.css" rel="stylesheet" type="text/css">
</head>

<body topmargin="0" bottommargin="0" rightmargin="0" onLoad="cambia(document.informe.diahasta, document.informe.meshasta);cambia(document.informe.diadesde, document.informe.mesdesde);">
        <form name="informe" id="informe" action="informe2.php" method="post">
        <tr bgcolor="#E9E9E9">
            <td class="txt5" colspan="5">&nbsp;</td>
        </tr>
        <tr bgcolor="#E9E9E9">
            <td class="txt5">&nbsp;</td>
            <td class="txt5">&nbsp;</td>
        </tr>
        <tr bgcolor="#E9E9E9">
                <td width="12" class="txt5">&nbsp;</td>
                <td width="201" class="txt5"><select name="diadesde" id="diadesde" class="txtarea" <?php echo $dis_desde?>><?php
                    
if ($_POST['action'] == "buscar") {
                        
$selected $_POST['diadesde'];
                    } else {
                        
$selected "01";
                    }
                    for(
$a=1;$a<=31;$a++) {
                        if (
$a 10) { $a "0".$a; }
                        echo 
"<option value=".$a;
                        if (
$a == $selected) { echo " selected"; unset($selected); }
                        echo 
">".$a."</option>";
                    }
                
?>
                </select> / <select name="mesdesde" class="txtarea" onChange="cambia(document.informe.diadesde, this)" <?php echo $dis_desde?>><?php
                    
if ($_POST['action'] == "buscar") {
                        
$selected $_POST['mesdesde'];
                    } else {
                        
$selected $fecha['mes'];
                    }
                    for(
$a=1;$a<=12;$a++) {
                        if (
$a 10) { $a "0".$a; }
                        echo 
"<option value=".$a;
                        if (
$a == $selected) { echo " selected"; unset($selected); }
                        echo
">".$a."</option>";
                    }
                
?>
                </select> / <select name="anodesde" class="txtarea" onChange="cambia(document.informe.diadesde, document.informe.mesdesde)"  <?php echo $dis_desde?>><?php
                    
if ($_POST['action'] == "buscar") {
                        
$selected $_POST['anodesde'];
                    } else {
                        
$selected $fecha['ano'];
                    }
                    for(
$a=2000;$a<=2020;$a++) {
                        if (
$a 10) { $a "0".$a; }
                        echo 
"<option value=".$a;
                        if (
$a == $selected) { echo " selected"; unset($selected); }
                        echo 
">".$a."</option>";
                    }
                
?>
                </select></td><td width="30"></td>
                <td width="158" class="txt5"><select name="diahasta" class="txtarea" <?php echo $dis_hasta?>><?php
                    
if ($_POST['action'] == "buscar") {
                        
$selected $_POST['diahasta'];
                    } else {
                        
$selected $fecha['dia'];
                    }
                    for(
$a=1;$a<=31;$a++) {
                        if (
$a 10) { $a "0".$a; }
                        echo 
"<option value=".$a;
                        if (
$a == $selected) { echo " selected"; unset($selected); }
                        echo 
">".$a."</option>";
                    }
                
?>
                </select> / <select name="meshasta" class="txtarea" onChange="cambia(document.informe.diahasta, this)" <?php echo $dis_hasta?>><?php
                    
if ($_POST['action'] == "buscar") {
                        
$selected $_POST['meshasta'];
                    } else {
                        
$selected $fecha['mes'];
                    }
                    for(
$a=1;$a<=12;$a++) {
                        if (
$a 10) { $a "0".$a; }
                        echo 
"<option value=".$a;
                        if (
$a == $selected) { echo " selected"; unset($selected); }
                        echo 
">".$a."</option>";
                    }
                
?>
                </select> / <select name="anohasta" class="txtarea" onChange="cambia(document.informe.diahasta, document.informe.meshasta)" <?php echo $dis_hasta?>><?php
                    
if ($_POST['action'] == "buscar") {
                        
$selected $_POST['anohasta'];
                    } else {
                        
$selected $fecha['ano'];
                    }
                    for(
$a=2000;$a<=2020;$a++) {
                        if (
$a 10) { $a "0".$a; }
                        echo 
"<option value=".$a;
                        if (
$a == $selected) { echo " selected"; unset($selected); }
                        echo 
">".$a."</option>";
                    }
                
?></select></td>
                <td class="txt5" colspan="2"><input type="button" value="Buscar" class="txt5" onClick="valFecha(informe.diadesde.value + '/' + informe.mesdesde.value + '/' + informe.anodesde.value, informe.diahasta.value + '/' + informe.meshasta.value + '/' + informe.anohasta.value)"></td>
            </tr>
        <input type="hidden" name="action" value="buscar">
        <input type="hidden" name="valido" value=0>
        </form>
</body>
</html>

Última edición por Seppo; 22/11/2005 a las 09:43
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




La zona horaria es GMT -6. Ahora son las 04:54.