Whats the correct format using the date() function in PHP to insert into a MySQL date type column? or am I using the wrong function? I need to be able to insert the current date into the database via a mysql_query.
EXAMPLE
$insert = mysql_query("INSERT INTO booking (test1,test2,test3,Date_Booked) VALUES ('". $var1. ",". $var2.",". $var3 .","date()"')");
Assuming your Date_Booked column is expecting a date value it would be date('Y-m-d'). A better alternative might be to use MySQL's own date functionality. Using CURDATE() or NOW() would also accomplish the same thing:
$insert = mysql_query("INSERT INTO booking (test1,test2,test3,Date_Booked)
VALUES ('". $var1. ",". $var2.",". $var3 .",NOW())");
$insert = mysql_query("INSERT INTO booking (test1,test2,test3,Date_Booked)
VALUES ('". $var1. ",". $var2.",". $var3 .",CURDATE())");
My code, I use date("Y-m-d H:i:s") for a field of type "DATETIME" but this will depend on the data structure of your table. There are other date string that may work depending. Can you provide the structure of the date field in your table so I can revise my answer if needed?
$insert =
mysql_query("INSERT INTO
booking (test1,test2,test3,Date_Booked)
VALUES
('$var1','$var2','$var3','".date('Y-m-d H:i:s')."')");
Related
I'm having trouble inserting a record with a date field. No matter what I do, the date field gets stored as '0000-00-00'. The value comes into the php script as a string in this format:
'9/13/2013'. I'm converting to a date using strtotime, then trying to insert the record. Here's the code. Why can't I get this to work? Thanks!
$my_date = strtotime($_POST['d']);
//die(date("Y-m-d", $my_date)); //<--outputs just fine if I uncomment this
$sql = "INSERT INTO locations (id, my_date) VALUES ('$id', '$my_date')";
$result = mysql_query($sql);
I'm converting to a date using strtotime
strtotime returns a unix timestamp (integer), not a date string. You can format it for MySQL before inserting with the date function:
$my_date = date('Y-m-d H:i:s', strtotime($_POST['d']));
Alternatively, you can use the DateTime class for this purpose.
i have a column in my database for the date a new record was added, it's set with the following code:
$dateset = date("Y-m-d");
$q = "INSERT INTO appts (date_set) VALUES ('$dateset')";
this is working about 80% of the time, is there any reason that randomly it will add the date as "0000-00-00"?
You should check type of field 'date_set'.
if type is DATE you have to use:
$dateset = date("Y-m-d");
if type is DATETIME you have to use:
$dateset = date("Y-m-d H:i:s");
//It is correct, but database push after time 00:00:00 (but it depends on the settings of the database)
//$dateset = date("Y-m-d");
if type is TIMESTAMP you have to use:
$dateset = time();
Try use sql:
$q = "INSERT INTO appts (date_set) VALUES (NOW())";
I think it would the best decision to use
$q = "INSERT INTO appts (date_set) VALUES (NOW())";
is there any reason that randomly it will add the date as "0000-00-00"?
No.
Either $dateset populated some other way or value got updated to 0's later on.
I have a mySQL table with a column formatted as "timestamp". I am trying to insert a value using a php script to no avail. Below I have shown a couple unsuccessful approaches, can any one demonstrate how to do generate a properly formatted value using php?
$insert = mysql_query("INSERT INTO `tableName`(`timeColumnName`) VALUES ('".strtotime("now")."')") or die(mysql_error());
$insert = mysql_query("INSERT INTO `tableName`(`timeColumnName`) VALUES (now())") or die(mysql_error());
You can use date() function in php, like this,
$insert = mysql_query("INSERT INTO tableName(timeColumnName)
VALUES ('".date("Y-m-d H:i:s")."')") or die(mysql_error());
This is a simple example for you to insert the timestamp. It depends which time format you would like to store in the MySQL DB.
$mysqldate = date( 'Y-m-d H:i:s', now() );
$query = "UPDATE table SET datetimefield = $mysqldate WHERE...";
If you wish to store the timestamp as a number, you have to change the type of your field in DB to number type.
$query_insert = "INSERT INTO users (user_name, user_pass, user_email, user_date, user_level, user_posts)
VALUES(?,?,?,?,?,?)";
$insert = $mysqli->prepare($query_insert);
$insert->bind_param("ssssii", $username, $password_enc, $email, $date, $level_start, $post_start);
$insert->execute();
$date = date('d-m-Y');
Those are the related parts. The only thing is...when i execute this it doesnt add to the table
I had a look around and someone said to have $insert->error; so I put that in and this came up
Incorrect datetime value: '31-01-2013' for column 'user_date' at row 1
Anybody able to help?
It has to be 2013-01-31, i.e. date('Y-m-d')
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html
when inserting value on date data type, it should be on the following format yyyy-MM-dd, so in your example date, it should 2013-01-31.
Don't worry about the pattern of dates when saved on the database. Leave it as is. If you are concern about the formatting, then you can do it during your projection of the data by using such function called DATE_FORMAT.
DATE_FORMAT()
I have table in mysql database which have field with datatype is datetime.
I want to save the datetime in this minute, for this I use " Now() ", but it does not work,
It just save 000000000000 in databaes.
If you use php, the correct format is:
date("Y-m-d H:i:s");
UPDATE:
Minutes are expressed as i not m
If you've got a timestamp in PHP check out FROM_UNIXTIME()
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
$tstamp = time();
$query = "INSERT INTO `table` VALUES (FROM_UNIXTIME($tstamp))";
INSERT ... (Now(), ...)
without additional quotes around the function Now()
I would use function time() too, then it's easy to output different kind of timestamps with date().
$query = "INSERT INTO tbl VALUES (".time().");";
date("g") this will return 12-hour format of an hour without leading zeros. For more options see http://php.net/manual/en/function.date.php
An example of a PHP script which sets a date in MySQL manually,
<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')”;
mysql_query($query_date) or die(mysql_error());
?>
An example of a PHP script which sets a date in MySQL Automatic,
<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )”;
mysql_query($query_date) or die(mysql_error());
?>
try this: set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:
INSERT INTO my_table (date_time) VALUES (CURRENT_TIMESTAMP)