how to insert a date selected in datepicker in a mySQL database - php

I've searched this and other sites, but I only find methods to insert a manual date value or to use a datepicker. Now i want to insert a date from my datepicker into a date field in mySQL.
$date = $_POST['calendar2b'];
$sql4 = "Insert into geschiedenis(login_fk, status_fk, code_fk, uitleendatum,inleverdatum,uitleenuur,inleveruur) Values ('".$login."','beschikbaar','".$code."',Now(),DATE_ADD(curdate(), INTERVAL 1 DAY),curtime(),curtime())";
If I use now this works, but I need to use the date from a datepicker.

Related

How to update mysql with current date with php?

I am trying to update a MySQL table with current date into a column which type is DATE. current date structure is (2014-05-15). I've tried a few ways but it is not updating the date, however it is updating other fields.
MySQL query.
$q = "UPDATE TABLE SET
DESCRIPTION =".$db->qstr($string).",
DATE =".$db->qstr(date('Y-m-d')).",
LAST_ACT =".$db->qstr( time())."
WHERE ID=".$db->qstr( $id);
I've tried DATE = DATE(NOW()), and DATE = DATE(), but it is not updating in MySQL,
Any suggestions?
Instead of DATE = DATE(NOW()).
Try DATE = NOW()
Try DATE=CURDATE() which will return a date data type. If that doesn't work, try doing some trial and error directly in the database console to see what works and what responses you get. And be sure and update us on the results.
It is very simple.
Use now() function for it.
So it will be :
DATE = now()

Storing Today's Date in MySQL Database

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

Date to Timestamp Help required

I am using jQuery DatePicker to select from date and to date. when I select a date i covert it to timestamp using strtotime function, I have some dates stored in my database with column type timestamp.
when I convert the selected date from date picker using strtotime
$from_date = strtotime($form['from-date']);
it gives me something like this
1338508800
while the date stored in my database table looks like this
2012-06-09 02:24:25
I want to query the records on the basis of the selected from and to date. but the query is not getting my dates correctly.
How can I change the selected date so that it give me results in between the two selected dates?
You want date('Y-m-d H:i:s',strtotime($form['from-date']));

How could I insert date from mktime into MySQL?

I want to insert date in format m-d into database. I used this code
$date1=date("m-d",mktime(0,0,0,$month1 ,$day1,1));
The date is inserted as 0000-00-00 in MySQL, and I don't know why.
I want to do that to compare that date stored in MySQL with current month and day. How can this be done?
mysql date must be of the form Y-m-d . Building a date with m-d form brings an invalid date.
If you want to compare only month or day, you can use mysql MONTH and DAYOFMONTH functions. For example:
$query = "SELECT * FROM table WHERE MONTH(datecol) = {$month1} AND DAYOFMONTH(datecol) = {$day1}"
$date1=date("Y-m-d",mktime(0,0,0,$month1 ,$day1,1));

How to put the current datetime in MySQL using PHP

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

Categories