How to put the current datetime in MySQL using PHP?
The result should be:
mysql_query("INSERT INTO users (r_date) VALUES ('the current datetime')");
The type of r_date is datetime.
If your field is a DATE or DATETIME, NOW() will work:
INSERT INTO users (r_date) VALUES (NOW());");
Note that the now() function insert current date of MySQL server.
If you need to insert server date you should use php date function
Related
When my html form is submitted, I want today's date also stored in one column along with the data given by the form in mysql table.
My php code:
#some code
$date = date('d-m-Y');
#some code
$sql = "INSERT INTO table1(rollNo, password, name, item, place, description, contact, date) VALUES('$rollNo', '$password', '$name', '$item', '$place', '$description', '$contact', '$date') ";
But for some reason, every time form is submitted, in the date column '0000-00-00' is stored instead of today's date. I tried using different formats(d/m/Y etc.), but didn't work. I have checked that in MySQL table, date column's type is date, not string. I am a newbie in php and MySQL and I don't know why this is happening.
Also, I want this page to daily(at 11:59 PM) send mail of that day's entries. For that, I am planning to check every entry's date with today's date, and send mail of only those that match. Please tell me if there is another simpler method of doing it.
EDIT:
Just to make it clear, date column's type is DATE.
Use:
`dateandtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
And using a simple substring, you can pick out the date (YYYY-MM-DD) with ease.
substr($sql['dateandtime'], 0, 10);
Use NOW() function of MySQL with column type DATETIME
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now
You can also get just the date:
http://www.w3schools.com/sql/func_curdate.asp
UPDATE table SET date = CURDATE();
I have what is most likely a very simple question.. I am designing a simple blogging system and I am trying to put the current date into the table where the blog post is stored whilst waiting for administrator approval. but the method I have used puts 0000-00-00 into the date column! What I am using is as follows:
$query = "INSERT INTO blogentry VALUES ('".$mnam."','".date('d-m-Y h:m:s') ."\n"."','".$mcom."','".$approve."')";
I am relatively new to php so stumble accross errors like this all the time... but I cant seem to google this one!
Thanks guys!
So the easiest way to do this is just let MySQL handle it with the NOW() function:
INSERT INTO blogentry VALUES( ..., NOW(), ... )
Another option is to use TIMESTAMPs by changing your table - set the column to type TIMESTAMP with DEFAULT CURRENT_TIMESTAMP, and you can just ignore that column when inserting - it will automatically be filled with the current time. You will need to specify the columns you're inserting to in order to skip a column:
INSERT INTO blogentry( column1, column2 ) VALUES( column1value, column2value )
Finally, you NEED to sanitize your inputs. Preferably using prepared statements and PDO (http://php.net/manual/en/pdo.prepared-statements.php), or at least using mysql_real_escape_string.
From the MySQL manual on DATE, DATETIME
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
This means you have to insert the dates in YYYY-MM-DD format. You are using date('d-m-Y h:m:s') format. Change that to date('Y-m-d') and it should insert correctly.
If you want the time as well, then you need to change the column datatype to DATETIME and then insert using the format date('Y-m-d H:i:s').
As other mention, you can use an INT column type instead and store a Unix timestamp which is stored in UTC so it is more portable. You can then easily manipulate the timestamp to output the date any way you would like.
Try just storing a strtotime() result. It creates a unique timestamp, which can then be parsed however you need it in the future.
You might need to give the timestamp to the date function:
date('d-m-Y h:m:s', strtotime('now'))
Also, to do a standard datetime format:
date('Y-m-d H:i:s', strtotime('now'))
I have a MySQL database. All the fields, I assign and I have a datestamp for my date field.
it automatically generates YYYY-MM-DD HH:MM:SS like this 2011-11-21 21:31:37
However, I would like it to do so in two diffrent columns:
A date field with YYYY-MM-DD or 2011-11-21
A time field with HH:MM:SS or 21:31:37
This is my insert php code
$sql= "INSERT INTO `db`.`table` (`id` ,`fkid` ,`paid` ,`date`)
VALUES (NULL, '$userid', '0', CURRENT_TIMESTAMP);";
I have tried CURRENT_DATESTAMP and it does not work.
The 2nd part of the question is: how to I make the table so it works with the proper code? Should the structure of the of the field be type text, or date?
I would not recommend splitting your timestamp into separate date and time columns. Instead, it is easiest to use a DATETIME column, and query it for its date and time portions using the MySQL functions DATE() and TIME():
SELECT DATE(`date`) AS d, TIME(`date`) AS t FROM db.dable;
When inserting, you can use the NOW() function to set the current timestamp. `CURRENT_TIMESTAMP() is a synonym for NOW().
$sql= "INSERT INTO `db`.`table` (`id` ,`fkid` ,`paid` ,`date`)VALUES (NULL , '$userid', '0', NOW());";
I use NOW() function but I get this weird date:
2011-11-06
How do I get the following European date:
ss:mm:hh dd:mm:year (06-11-2011)
In my database I set the field column date as DATE
How do you integrate DATEFORMAT into this query:
$avatar_Q=mysql_query("
SELECT user_name,avatar,reputation,comment, DATE_FORMAT(date,'%d/%m/%Y %h:%i') AS rightNow
FROM comments AS com
INNER JOIN users AS us ON com.user_id=us.user_id
WHERE comment_id=$commentID
") or die(mysql_error());
The date is is in the column table
MySQL uses the following date format - YYYY-MM-DD - if you want a different format you need to use the DATE_FORMAT function on select
for example :
SELECT DATE_FORMAT(datecolumn,"%d/%m/%Y %h:%i")
FROM atable
To integrate the date_format function into your select statement you need to list the fields individually
Yep, use a Timestamp column and insert with CURRENT_TIMESTAMP. It saves a lot of time! :)
My personal recommendation is to save the date as a UNIX Timestamp (using the time() function) ,
This way you could format it as you wish .
Shai.
DATE_FORMAT
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
That format is just how MySQL stores the data type DATE: http://dev.mysql.com/doc/refman/5.5/en/datetime.html.
If you're using the DATE data type for you column you can either:
Use the DATE_FORMAT function in your SQL to get the date in your desired format
Use a combination of PHP's strtotime and date functions to display your date in the most appropriate format
I am trying to set the value of a field via a hidden form field to the current date and time using either PHP or Javascript that would conform to MySQL's datetime field.
You can use PHP to get and format the current system date/time for use in MySQL like this:
$now = date('Y-m-d H-i-s');
You can directly set current date and time in your SQL insert query using NOW():
INSERT INTO table_name (current_time, column2, column3,...)
VALUES (NOW(), value2, value3,...)
where current_time is the field where you want to put current date and time.
Create the column using DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP
Those together will make it so that any new rows inserted have the current time and are updated again when the column is updated.
Example:
CREATE TABLE test (last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
Edit: Nevermind, this will use a TIMESTAMP column, not DATETIME. Other answers will do what you want.
<?php echo time(); ?>
will output a nice simple integer number that you can pass directly into MySQL and convert into a native mysql datetime value with FROM_UNIXTIME(). It'll save you the trouble of formatting the data in a nice YYYY-MM-DD hh:mm:ss string.
$current = date_timestamp_set(date_create(), time());