Foros del Web » Programando para Internet » PHP »

Fecha y hora actual en formulario PHP

Estas en el tema de Fecha y hora actual en formulario PHP en el foro de PHP en Foros del Web. Buenas al crear un formulario en php como puedo insertar la fecha y hora actual para que se almacene en el campo DATETIME de mysql. ...
  #1 (permalink)  
Antiguo 28/10/2007, 11:49
 
Fecha de Ingreso: agosto-2006
Mensajes: 173
Antigüedad: 17 años, 8 meses
Puntos: 1
Fecha y hora actual en formulario PHP

Buenas al crear un formulario en php como puedo insertar la fecha y hora actual para que se almacene en el campo DATETIME de mysql.

Muchas gracias
  #2 (permalink)  
Antiguo 28/10/2007, 11:58
Avatar de Seppo  
Fecha de Ingreso: marzo-2005
Ubicación: Buenos Aires, Argentina
Mensajes: 1.284
Antigüedad: 19 años, 1 mes
Puntos: 17
Re: Fecha y hora actual en formulario PHP

Código PHP:
<?php
mysql_query
('INSERT INTO tabla SET campo = "' date('Y-m-d H:i:s') . ");
?>
  #3 (permalink)  
Antiguo 28/10/2007, 12:25
Avatar de fearlex  
Fecha de Ingreso: septiembre-2003
Ubicación: Exiliado de mi patria
Mensajes: 1.047
Antigüedad: 20 años, 7 meses
Puntos: 5
Re: Fecha y hora actual en formulario PHP

O asi:

Código PHP:
<?php
mysql_query
('INSERT INTO tabla SET campo = now()');
?>
__________________
Ya viene llegando, el comunismo en Cuba, ya se esta acabando.
www.fearlex.com | www.fearlexworld.com
  #4 (permalink)  
Antiguo 28/10/2007, 12:57
 
Fecha de Ingreso: agosto-2006
Mensajes: 173
Antigüedad: 17 años, 8 meses
Puntos: 1
Re: Fecha y hora actual en formulario PHP

Muchas gracias a ambos
  #5 (permalink)  
Antiguo 28/10/2007, 13:23
 
Fecha de Ingreso: agosto-2006
Mensajes: 173
Antigüedad: 17 años, 8 meses
Puntos: 1
Re: Fecha y hora actual en formulario PHP

Soy un poco torpe

He introducido las lineas y me crea un registro sin datos pero con la fecha y el otro registro con los datos y la fecha 0000-00-00 00:00:00

Código PHP:
<?php
function GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  
$insertSQL sprintf("INSERT INTO feedsdeportivos (fecha, satelite, frecuencia, polaridad, symbolrate, fec, categoria, transmision, informacion) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString($_POST['fecha'], "date"),
                       
GetSQLValueString($_POST['satelite'], "text"),
                       
GetSQLValueString($_POST['frecuencia'], "text"),
                       
GetSQLValueString($_POST['polaridad'], "text"),
                       
GetSQLValueString($_POST['symbolrate'], "text"),
                       
GetSQLValueString($_POST['fec'], "text"),
                       
GetSQLValueString($_POST['categoria'], "text"),
                       
GetSQLValueString($_POST['transmision'], "text"),
                       
GetSQLValueString($_POST['informacion'], "text"));

  
mysql_select_db($database_feedsdeportivos$feedsdeportivos);
  
$Result1 mysql_query($insertSQL$feedsdeportivos) or die(mysql_error());

  
$insertGoTo "formulario.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
    
$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$insertGoTo));
}
?>
Os pongo el codigo que tengo como formulario, por que no se como meterlo

Gracias
  #6 (permalink)  
Antiguo 28/10/2007, 14:49
 
Fecha de Ingreso: agosto-2006
Mensajes: 173
Antigüedad: 17 años, 8 meses
Puntos: 1
Re: Fecha y hora actual en formulario PHP

Me respondo a mi mismo, jejeje lo he conseguido

Código PHP:
<?php
function GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
      case 
"time":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  
$insertSQL sprintf("INSERT INTO feedsdeportivos (fecha, hora, satelite, frecuencia, polaridad, symbolrate, fec, categoria, transmision, informacion) VALUES (now(), now(), %s, %s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString($_POST['satelite'], "text"),
                       
GetSQLValueString($_POST['frecuencia'], "text"),
                       
GetSQLValueString($_POST['polaridad'], "text"),
                       
GetSQLValueString($_POST['symbolrate'], "text"),
                       
GetSQLValueString($_POST['fec'], "text"),
                       
GetSQLValueString($_POST['categoria'], "text"),
                       
GetSQLValueString($_POST['transmision'], "text"),
                       
GetSQLValueString($_POST['informacion'], "text"));


  
mysql_select_db($database_feedsdeportivos$feedsdeportivos);
  
$Result1 mysql_query($insertSQL$feedsdeportivos) or die(mysql_error());

  
$insertGoTo "formulario.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
    
$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s"$insertGoTo));
}
?>
Dejo el codigo, por si le puede servir a alguien, esto es lo que me faltaba

$insertSQL = sprintf("INSERT INTO feedsdeportivos (fecha, hora, satelite, frecuencia, polaridad, symbolrate, fec, categoria, transmision, informacion) VALUES (now(), now(), %s, %s, %s, %s, %s, %s, %s, %s)",
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 17:40.