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();
Related
$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)
want to be able to store the current date from PHP in to a mysql table.
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."','".$date."','".$yes."')";
Date's column type in database is DATE.
but date save in database like this: 0000-00-00
help me.
try this
$date = sprintf("%'04s-%'02s-%'02s",$year_number,$month_number,$day_number);
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`) VALUES ('".$ID."','".$credit."',".$date.",'".$yes."')";
or
if you want to inset today's date use
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
I need to store Date field in database for example VARCHAR(10) becouse, DATE type in mysql server don't support persian (farsi) date.
If you are getting the current date, just use one of the built-in MySQL functions for that:
$sql="INSERT INTO `prg omran`.`paid`(`Comapny_id`,`Amount`,`Date`,`Creditrecord`)
VALUES ('".$ID."','".$credit."',curdate(),'".$yes."')";
You won't ever have to worry about formatting or any sort of funny inputs and you can still do all sorts of addition, subtraction and the like using one of the many other functions.
Edit: I have just looked up some information on the date system and you will run into problems trying to squeeze that data into a date field. The first six months have 31 days, the next 5 have 30 days, then the last has 29 - unless it is a leap year in which case it has 30. The date field simply won't LET you insert these values into a date field.
You might need to convert your data to a timestamp and write a function to encode/decode it into the format you need.
I have the following code in php that inserts a start date and end date with the date and time . The date and time is added via the iphone date and time wheel when selecting the input field. The problem however is that if I select 1pm local time as the start date and 2pm as the end date it will submit the record as 6pm and 7pm. Is this a coding issue, a date/time field issue or a form issue? The variables of $start_date and $end_date are pulled directly from the input form.
$query = "INSERT INTO events (event_id, event_name, user_name, id_user, start_date, end_date, details, location, dresscode,private) VALUES ('NULL', '".$event_name."', '".$user_name."', '".$user_id."', '".$start_date."','".$end_date."','".$time."','".$location."','".$dresscode."','".$private_id."')";
mysql_query ($query);
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'))
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