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.
Related
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')."')");
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'm writing a PHP/MySQL program. I need to add the current date each time a new record to the TABLE is added. I have a field called DATE_ADDED. I'm confused how to use CURDATE() function from MySQL to write this to the TABLE. Or should I from PHP use a date function to get today's date and pass it as a variable to be written to the TABLE? I don't need a TIMESTAMP, just YYYY-MM-DD.
Thanks!
You have to try with php like
$today = date("Y-m-d");
and then while adding data to your db give this value with the remaining
INSERT INTO table_name ( field1, field2,...DATE_ADDED )
VALUES
( value1, value2,...CURDATE());
You can set it as the default value for that column date_added in the table definition like so:
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
$regdate=date('Y-m-d');
$sql = "INSERT INTO table_name (fld_name1, fld_name2,fld_regdate) VALUES ('value1', 'value2', '$regdate')";
$rs = mysql_query($sql) or die('ERROR:' mysql_error());
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();
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)