save datetime in mysql using php - php

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)

Related

date() function in PHP

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')."')");

PHP Insert Query - Date field 0000-00-00

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.

Inserting a "timestamp" formated entry to an mySQL table using php

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.

Store php datetime in mysql database

I can't believe I can't do this, but I want to be able to store the current date and time from php in to a mysql table.
The column in the table is type datetime.
I've tried this
$current_date = date("Y-m-d");
$my_date = strtotime($current_date);
INSERT INTO my_table (date_time) VALUES ('$my_date')
but my timestamp comes up as 0000-00-00 00:00:00
This must be so easy to do but I just can't get it working!
I want to use the timestamp from php rather than using the mysql now() function
Try this:
$my_date = date("Y-m-d H:i:s");
INSERT INTO my_table (date_time) VALUES ('$my_date');
In the date-format parameter of the date function, use :
'H' for 24hr format
'h' for 12hr format
Don't save it as the Unix Timestamp (which strtotime() outputs), but as "2012-12-02 13:00" into the DATETIME column.
Create column type TIMESTAMP and set it to NOT NULL. Then pass in NULL during INSERT and MySQL will insert current date and time. This works for me.
set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:
INSERT INTO my_table (`date_time`) VALUES (CURRENT_TIMESTAMP)
If you have the date in PHP as a timestamp, you can use the FROM_UNIXTIME function [1]
mysql> insert into table_name values (FROM_UNIXTIME(your_timestamp_here));
Hope it helped
[1]. https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
Remove the strtotime()
$current_date = date("Y-m-d");
INSERT INTO my_table (date_time) VALUES ('$current_date')
If you want to include the hour, minutes and seconds,
$current_date = date("Y-m-d H:i:s");

Within PHP should I use today's date function or curdate() from MySQL?

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();

Categories