$sql = "INSERT INTO Candidate_info(Name,Age,Party,Symbol,Type_election,Region,Constituency,Segment) VALUES ('$cn', '$age', '$np', '$content','$radio','$region','$co','$as')";
I want to insert users in the database but only on today's date.**
Did you mean you want to insert Current Date along with your other values
you could choose either of fallowing based on your need:
1) Create an additional column and set the default value to TIMESTAMP (can also set for Auto update on update if needed) you can also push date from php to override by passing date("Y-m-d H:i:s")
2) insert with php date("Y-m-d") to a varchar or date type col (use date recomended)
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 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());
I want to store the data and time in my MYSQL db.I have a datetime field in my db
I want to store current datatime in my db
How shold i get the current date time?How to pass it to db via a sql query
How can i then retriev an print it in correct yyyy--dd--mm format or any other format
What wil be format of time? wil it be 23 hrs etc?
How do i print date and time?
You can let MySQL determine the current timestamp by using Now() in your query.
INSERT INTO foo (dtfield) VALUES (Now())
This can also be done with a default value for a TIMESTAMP field in your table definition
CREATE TABLE foo (
id int auto_increment,
creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
v int,
primary key(id)
)
You could use MySQL's built in date/time functions if you only want to insert it into the MySQL database.
Have a look at: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Otherwise, you can use the PHP's date() function.
Assuming table named 'items' and field named 'modified' of type 'timestamp'
$r = mysql_query("INSERT INTO items (modified, x, ...) VALUES (CURRENT_TIMESTAMP, $x, ...)");
// (or "UPDATE items SET modified=CURRENT_TIMESTAMP, x=$x, ...)
...
$r = mysql_query("SELECT UNIX_TIMESTAMP(modified) FROM items");
$item = mysql_fetch_assoc($r);
$formatted_ts = date('g:ia', $item['modified']); // or another format *
you'll need to add appropriate error-checking which I've omitted; also need to adjust for consideration of timezones, which I've also left out
see date()
You might also want to set your timezone.