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')";
Related
I have this SQL PHP code where I insert the NOW() time:
$sql = "INSERT INTO logs (username, msg, color, Time) VALUES ('$uname', '$msg', '$color', NOW())";
Unfortunately, it gives me GMT rather than Central European Time. What can I do? I tried putting date_default_timezone_set('Europe/Vienna'); Before it but it won't work.
Get the time with PHP and insert inside the database. Servers time is diffrent as they may located at diffrent locations. In your case the value of timestamp will be inserted by the server.
date_default_timezone_set('Europe/Vienna');
$time = date('d-m-Y H:i:s');
$sql = "INSERT INTO logs (username, msg, color, Time) VALUES ('$uname', '$msg', '$color', '$time')";
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 am building a simple php script to manage a blog, but i am stack..
Right now is possible to insert in a form the: name, mail, content.
And the comment will be display properly.
I would like to attach to every comment the exact date and time of the comment.
I pass all the afternoon reading about date() time() and timestamp() but i don't get anything out of it.
Someone can help me to add the date and time to the comments, in a simple way?
Thank you!!
the query right now is just this:
$query = "INSERT INTO posts (name, mail, content) VALUES
('$this->name', '$this->mail', '$this->content')";
Add a DATETIME column to your table. Insert date('Y-m-d H:i:s) into that column when you're inserting the comment. Then you can parse that time (if you wish to display it in any other way than the YYYY-MM-DD HH:MM:SS format. There's a couple of ways you can do this, some suggestions are with the strtotime and date or strftime functions or the DateTime class.
Update
$query = "INSERT INTO posts (name, mail, content, datetime) VALUES ('$this->name', '$this->mail', '$this->content', " . date('Y-m-d H:i:s) . ")";
The best way is use a timestamp column in the table that stores comments.
This way you don't have to do anything in your insert statement (DB does it for you).
Then, when you want to get that date, you must parse the value but it depends on which DataBase are you using.
$query = "INSERT INTO posts (name, mail, content, datetime) VALUES ('$this->name', '$this->mail', '$this->content', " . date('Y-m-d H:i:s) . ")";
To read timestamp(), use this code
echo date('Y-m-d',strtotime(date('Y-m-d H:i:s'))); // ---->2013-06-22
// echo date('Y-m-d H:i:s'); ---->2013-06-22 18:03:23
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 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.