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);
Related
I am working on a school website that was working but is now having errors. The page attempts to insert a new exam into our database of exams, but on submission hits this error.
"Error SQLSTATE[HY000]: General error: 1411 Incorrect datetime value: '2:50 AM' for function str_to_date"
The code we are using to input the data is as follows
$sqlInsertExam = "INSERT INTO exam (name, quarter, date, location, state, possible_grade, passing_grade, duration, start_time, cutoff)
VALUES (:name, :quarter, :exam_date, :location, :state, :possible_grade, :passing_grade, :duration, STR_TO_DATE(:start_time, '%h:%i %p'), :cutoff)";
as you can see we are using %h:%i %p which as far as I know means in 12 hour format hr:min AM/PM. Which is what we are giving it. The only problem I can see is that the previous datetimes in the table are listed as military time in the form hr:mm:ss e.g. 14:00:00. But I am unsure if that is what is causing this error
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'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.
I have a site where users can update their status. Knowing the date/time they posted the status is very important. I create a field in my table and gave it the date time type.
I am new to working with dates/times. I know the structure for the datetime field is this:
YYYY-MM-DD HH:MM:SS
I thought doing this in my INSERT query would work, but It didnt.
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story',
'date()', '1')";
When I check the database table the post is successful, but the datetime reads
0000-00-00 00:00:00
What can I do to achieve my end result. This whole unix time stamp and stuff with date/time really confuses me. Thanks.
Don't quote the mysql function, and you should use NOW() instead of DATE().
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story', NOW(), '1')";
Give this a try, this will add the record to the database with the current date and time in the format you posted:
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story', now(), '1')";
EDIT: Posted same time as above.
Lets tr this:
$date_time = NOW();
then
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story',$date_time, 'My_View')";
I want to save the date and time from PHP to SQL. Here is the SQL statement to insert new record (found in a method within a class):
INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
value (:headline, :text, :date, :rating, :product_id, :username)
And in my .php page, I call the current date and time using $_SERVER['REQUEST_TIME']. But still I'm getting the error "Incorrect datetime value". What can I use to get the date?
Your timestamp can be generated:
$timestamp = date('Y-m-d H:i:s');
This should mimic the mysql timestamp and datetime formats.
Assuming that the mysql has its timestamp synchronized with the php server in question, you can also just use the mysql current timestamp functions:
NOW() or CURRENT_TIMESTAMP or CURRENT_TIMESTAMP()
Does it have to be the exact request time? You could make your life easier and simply use:
INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
value (:headline, :text, now(), :rating, :product_id, :username)
MySQL inserts the current date as soon your entry is written to the table.
it is only a guess, but $_SERVER['REQUEST_TIME'] returns a float, not a valid datetime format.
If you need the current time you can do it like that:
INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
INSERT INTO tbl_reviews ('$headline', '$text', CURDATE(), '$rating', '$product_id', '$username');
date_default_timezone_set('US/Eastern');
$cur_date=date("Y-m-d");
You can use SQL's own CURRENT_TIMESTAMP value to get an automatically formatted timestamp.
As for $_SERVER['REQUEST_TIME'], you can just use time() or microtime() instead.
As mentioned, REQUEST_TIME, as well as time() and microtime() return a UNIX timestamp, which is basically the amount of seconds that have passed since the UNIX epoch, which is not the same format as a DATETIME field expects.