Foros del Web » Programando para Internet » PHP »

Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Estas en el tema de Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql) en el foro de PHP en Foros del Web. Espero me ayuden a solucionar ete problema de fechas para guardarlas en una bases de datos MySql, al colocar la funcion que debe realizar el ...
  #1 (permalink)  
Antiguo 29/03/2007, 22:41
 
Fecha de Ingreso: marzo-2007
Mensajes: 6
Antigüedad: 17 años
Puntos: 0
Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Espero me ayuden a solucionar ete problema de fechas para guardarlas en una bases de datos MySql, al colocar la funcion que debe realizar el cambio de fecha para almacenarlo enla base de datos no me devuelve ningún error pero enla base de datos se graba 0000-00-00 y no la fecha ingresada, no se en donde esta el error?

este es el php subrayado en rojo el scritp para el cambio de fecha:

<?php
//set the database connection variables
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbDatabase = "moisesgo_euro";
//connet to the database
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

// Define post fields into simple variables

$first_name = $_POST['first_name'];

$last_name = $_POST['last_name'];

$email = $_POST['email'];

$phone = $_POST['phone'];

$date_pick = $_POST['date_pick'];

$hour_pick = $_POST['hour_pick'];

$place_pick = $_POST['place_pick'];

$airline = $_POST['airline'];

$flight = $_POST['flight'];

$date_drop = $_POST['date_drop'];

$hour_drop = $_POST['hour_drop'];

$place_drop = $_POST['place_drop'];

$id_easy = $_POST['id_easy'];

$vehicle = $_POST['vehicle'];

/* Let's strip some slashes in case the user entered

any escaped characters. */

$first_name = stripslashes($first_name);

$last_name = stripslashes($last_name);

$email = stripslashes($email);

$phone = stripslashes($phone);

$date_pick = stripslashes($date_pick);

$hour_pick = stripslashes($hour_pick);

$place_pick = stripslashes($place_pick);

$airline = stripslashes($airline);

$flight = stripslashes($flight);

$date_drop = stripslashes($date_drop);

$hour_drop = stripslashes($hour_drop);

$place_drop = stripslashes($place_drop);

$id_easy = stripslashes($id_easy);

$vehicle = stripslashes($vehicle);


/* Do some error checking on the form posted fields */

if((!$first_name) || (!$last_name) || (!$email) || (!$phone) || (!$date_pick) || (!$hour_pick) || (!$place_pick) || (!$date_drop) || (!$hour_drop) || (!$place_drop) || (!$vehicle)){

echo '<center><basefont size="2" face="Arial, Helvetica, sans-serif" color="#000000">You did not submit the following required information!</center>';

if(!$first_name){

echo "<center>First Name is a required field. Please enter it below.</center>";

}

if(!$last_name){

echo "<center>Last Name is a required field. Please enter it below.</center>";

}

if(!$email){

echo "<center>Email Address is a required field. Please enter it below.</center>";

}

if(!$phone){

echo "<center>Phone Number is a required field. Please enter it below.</center>";

}

if(!$date_pick){

echo "<center>Date Pick is a required field. Please enter it below.</center>";

}

if(!$hour_pick){

echo "<center>Hour Pick is a required field. Please enter it below.</center>";

}

if(!$place_pick){

echo "<center>Place Pick is a required field. Please enter it below.</center>";

}

if(!$date_drop){

echo "<center>Date_drop is a required field. Please enter it below.</center>";

}

if(!$hour_drop){

echo "<center>Hour Drop is a required field. Please enter it below.</center>";

}

if(!$place_drop){

echo "<center>Place Drop is a required field. Please enter it below.</center>";

}


if(!$vehicle){

echo "<center>Select a Vehicle Type is a required field. Please enter it below.</center>";

}

include 'ilus_cen.htm'; // Show the form again!

/* End the error checking and if everything is ok, we'll move on to

creating the user account */

exit(); // if the error checking has failed, we'll exit the script!

}

// Enter info into the Database.

$info2 = htmlspecialchars($info);

function fecha_mysql($date_pick)
{
ereg( "([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})", $date_pick, $mifecha);
$fecha_lista=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1];
return $fecha_lista;
}



$sql = mysql_query("INSERT INTO reservations (first_name, last_name,

email, phone, date_pick, hour_pick, place_pick, airline, flight,

date_drop, hour_drop, place_drop, id_easy, vehicle)

VALUES('$first_name', '$last_name', '$email',

'$phone', '".fecha_mysql($date_pick)."', '$hour_pick', '$place_pick', '$airline', '$flight',

'$date_drop', '$hour_drop', '$place_drop', '$id_easy', '$vehicle')")

or die (mysql_error());

if(!$sql){

echo '<center><basefont size="2" face="Arial, Helvetica, sans-serif" color="#000000">There has been an error creating your account. Please contact the webmaster.</center>';

} else {

$reser_id = mysql_insert_id();

// Let's mail the user!

$subject = "Euro American Reservation - $last_name";

$message = "<html>
<head>
<title>Euro American Reservation</title>
</head>
<body>
Thank you for choosing Euro American Rent-A-Car,
<br>
<br>
Confirmation: $reser_id Status: Confirmed <br>
<br>
<strong>Renter Information</strong><br>
<strong>Name:</strong> $first_name<br>
<strong>Last Name:</strong> $last_name<br>
<strong>Email:</strong> $email<br>
<strong>Phone:</strong> $phone
</p>

<p><strong>Pick Up Information</strong><br>
<strong>Date Pick Up:</strong> $date_pick<br>
<strong>Hour Pick Up:</strong> $hour_pick<br>
<strong>Place Pick up:</strong> $place_pick<br>
<strong>Airline:</strong> $airline<br>
<strong>Flight:</strong> $flight</p>

<p><strong>Drop Off Information</strong><br>
<strong>Date Drop Off:</strong> $date_drop<br>
<strong>Hour Drop Off:</strong> $hour_drop<br>
<strong>Place Drop Off: </strong>$place_drop</p>

<p><strong>Id Client: </strong>$id_easy</p>
<p><strong>Vehicle: </strong>$vehicle</p>

<p>Customer Service<br>
Euro American<br>
Rent-A-Car - Tours <br>
This is an automated response, please do not reply!&quot;</p>
</body>
</html>";

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EuroAmerican Reservations <[email protected]>' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

mail($email, $subject, $message, $headers);

echo 'Your confirmation has been mailed to your email address!

exit;
}
?>
  #2 (permalink)  
Antiguo 30/03/2007, 00:21
 
Fecha de Ingreso: abril-2006
Ubicación: Colombia
Mensajes: 648
Antigüedad: 18 años
Puntos: 3
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Hola

Bueno, si pruebas la función antes del insert into te imprime correctamente?? si es asi, entonces en el insert into deja la funcion asi

$variable, 'fecha_mysql($date_pick)',$otravariable

si no te funciona asi entonces asigna en una variable php esa funcion y luego si la envias al insert into asi

$fec = fecha_mysql($date_pick);
insert into tabla (campos) values ('$variable','$fec','$otravariable');

espero te sirva.
__________________
Suerte.
_______________________________
"La vida es el principio de la muerte".
  #3 (permalink)  
Antiguo 30/03/2007, 08:25
Avatar de PequeñoMauro  
Fecha de Ingreso: abril-2006
Ubicación: Lima - Perú
Mensajes: 600
Antigüedad: 18 años
Puntos: 3
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Creo que no era necesario poner todo el código
__________________
"Nada en mis manos traigo tan solo a tu cruz me aferro....."
  #4 (permalink)  
Antiguo 30/03/2007, 09:48
 
Fecha de Ingreso: abril-2006
Ubicación: Colombia
Mensajes: 648
Antigüedad: 18 años
Puntos: 3
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

???????

Cita:
Iniciado por PequeñoMauro Ver Mensaje
Creo que no era necesario poner todo el código
__________________
Suerte.
_______________________________
"La vida es el principio de la muerte".
  #5 (permalink)  
Antiguo 30/03/2007, 10:08
 
Fecha de Ingreso: marzo-2007
Mensajes: 6
Antigüedad: 17 años
Puntos: 0
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Probare... gracias, si funciona bien prometo no poner todo el código....
  #6 (permalink)  
Antiguo 30/03/2007, 11:25
Avatar de BrujoNic
Super Moderador
 
Fecha de Ingreso: noviembre-2001
Ubicación: Costa Rica/Nicaragua
Mensajes: 16.935
Antigüedad: 22 años, 5 meses
Puntos: 655
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Movido desde BD a PHP.

Función de la sección de Base de Datos
__________________
La tecnología está para ayudarnos. No comprendo el porqué con esa ayuda, la gente escribe TAN MAL.
NO PERDAMOS NUESTRO LINDO IDIOMA ESPAÑOL
  #7 (permalink)  
Antiguo 01/04/2007, 03:10
 
Fecha de Ingreso: marzo-2007
Mensajes: 6
Antigüedad: 17 años
Puntos: 0
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Trate de hacerlo, pero no me graba la fecha en la base de datos, al colocar la función el Php se daña y deja de enviar los datos a la base, soy un diseñador y trato de utilizar el php, no logro entender el porque no funciona ya que todo se mira de acuerdo a un ejemplo que encontre, hice como me comentaste crear una variable para enviar la función a la base pero no logro hacer que pase nada, si pueden ayudarme adjunto el código:

$fecha = $_POST['fecha'];
function cambiaf_ a_mysql($fecha){
ereg( "([0-9]{1,2})/([0-9]{1,2})/([0-9]{2,4})", $fecha, $mifecha);
$lafecha=$mifecha[3]."-".$mifecha[2]."-".$mifecha[1];
return $lafecha;
}
$fecha1 = cambiaf_ a_mysql($fecha);
mysql_select_db($database_fechas, $fechas);
$insertSQL = sprintf("INSERT INTO date (fecha) VALUES($fecha1)");
  #8 (permalink)  
Antiguo 01/04/2007, 11:46
 
Fecha de Ingreso: abril-2006
Ubicación: Colombia
Mensajes: 648
Antigüedad: 18 años
Puntos: 3
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Hola

Bueno, creo que te hace falta el mysql_query e iria asi

mysql_query("INSERT INTO date (fecha) values ('$fecha1')");
espero te sirva
__________________
Suerte.
_______________________________
"La vida es el principio de la muerte".
  #9 (permalink)  
Antiguo 01/04/2007, 13:18
 
Fecha de Ingreso: abril-2003
Mensajes: 587
Antigüedad: 21 años
Puntos: 5
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

LO insertas vacio y en el campo fecha en la mysql lo pones como TIMESTAP
__________________
Si la vida te da la espalda tocale el culo
  #10 (permalink)  
Antiguo 01/04/2007, 19:28
 
Fecha de Ingreso: marzo-2007
Mensajes: 6
Antigüedad: 17 años
Puntos: 0
Re: Cambiar fechas dd-mm-yyyy(PHP) a yyyy-mm-dd(MySql)

Encontre una manera más amigable de lograr insertarla es la siguiente:

$fecha=$_POST['fecha']
$fch=explode("/",$fecha);
$fecha=$fch[2]."-".$fch[1]."-".$fch[0];

para pasar a a mysql
INSERT INTO tabla VALUES ('campo1','$fecha','campo2')

Gracias por su ayuda y comparto el código por si alguen lo necesita
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 08:22.