Ver Mensaje Individual
  #1 (permalink)  
Antiguo 21/02/2012, 06:13
carreteboy
 
Fecha de Ingreso: febrero-2012
Mensajes: 40
Antigüedad: 12 años, 2 meses
Puntos: 0
Pregunta insertar fechas desde php a mysql

Hola a todos, en el foro he encontrado este código para insertar fechas a mysql
Código PHP:
Ver original
  1. <?php
  2. class formato_fecha{
  3. ////////////////////////////////////////////////////
  4. //Convierte fecha de mysql a normal
  5. ////////////////////////////////////////////////////
  6. function cambiaf_a_normal($date){
  7. # ================================================== ========
  8. # ==== Recibe una fecha con formato aaaa-mm-dd hh:mm:ss ====
  9. # ==== Devuelve una fecha con formato dd-mm-aa ====
  10. # ================================================== ========
  11.  
  12. $year=substr($date,0,4);
  13. $month=substr($date,5,2);
  14. $day=substr($date,8,2);
  15. $date=$day."-".$month."-".$year;
  16. return ($date);
  17. }
  18.  
  19. ////////////////////////////////////////////////////
  20. //Convierte fecha de normal a mysql
  21. ////////////////////////////////////////////////////
  22.  
  23. function cambiaf_a_mysql($date){
  24. # ================================================== ========
  25. # ==== Recibe una fecha con formato dd-mm-aa ====
  26. # ==== Devuelve una fecha con formato aaaa-mm-dd hh:mm:ss ====
  27. # ================================================== ========
  28.  
  29. $day=substr($date,0,2);
  30. $month=substr($date,3,2);
  31. $year=substr($date,6,4);
  32. $date=$year."-".$month."-".$day;
  33. return ($date);
  34. }
  35. }
  36. ?>
El problema es cómo lo utilizo porque mis fechas las ingreso desde un formulario

Código HTML:
Ver original
  1. <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  2.   <table align="center">
  3.     <tr valign="baseline">
  4.       <td nowrap="nowrap" align="right">Id_postulante:</td>
  5.       <td><input type="text" name="id_postulante" value="" size="32" /></td>
  6.     </tr>
  7.     <tr valign="baseline">
  8.       <td nowrap="nowrap" align="right">Id_tausencia:</td>
  9.       <td><input type="text" name="id_tausencia" value="" size="32" /></td>
  10.     </tr>
  11.     <tr valign="baseline">
  12.       <td nowrap="nowrap" align="right">Fecha_inicio:</td>
  13.       <td><input type="text" name="fecha_inicio" value="" size="32" /></td>
  14.     </tr>
  15.     <tr valign="baseline">
  16.       <td nowrap="nowrap" align="right">Fecha_termino:</td>
  17.       <td><input type="text" name="fecha_termino" value="" size="32" /></td>
  18.     </tr>
  19.     <tr valign="baseline">
  20.       <td nowrap="nowrap" align="right">Num_dias:</td>
  21.       <td><input type="text" name="num_dias" value="" size="32" /></td>
  22.     </tr>
  23.     <tr valign="baseline">
  24.       <td nowrap="nowrap" align="right">Obs:</td>
  25.       <td><input type="text" name="obs" value="" size="32" /></td>
  26.     </tr>
  27.     <tr valign="baseline">
  28.       <td nowrap="nowrap" align="right">&nbsp;</td>
  29.       <td><input type="submit" value="Insertar registro" /></td>
  30.     </tr>
  31.   </table>
  32.   <input type="hidden" name="MM_insert" value="form1" />
  33. </form>

y aquí va mi insert

Código MySQL:
Ver original
  1. $insertSQL = sprintf("INSERT INTO ausencias (id_postulante, id_tausencia, fecha_inicio, fecha_termino, num_dias, obs) VALUES (%s, %s, %s, %s, %s, %s)",
  2.                        GetSQLValueString($_POST['id_postulante'], "int"),
  3.                        GetSQLValueString($_POST['id_tausencia'], "int"),
  4.                        GetSQLValueString($_POST['fecha_inicio'], "date"),
  5.                        GetSQLValueString($_POST['fecha_termino'], "date"),
  6.                        GetSQLValueString($_POST['num_dias'], "int"),
  7.                        GetSQLValueString($_POST['obs'], "text"));