PHP: blog - how to add the date to a comment - php

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

Related

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

PHP - Putting a date into a MySQL table

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'))

Save date/time from PHP to SQL

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.

Insert current date to the database?

How do I insert the current date to my database? I have a column called date to put it on.
I want to insert it at the same time I insert this:
$sql="INSERT INTO `Lines` (Text, PID, Position)
VALUES
('$text','$pid','$position')";
Is there a way to automate it in PHPMyAdmin or it's the same to do it this way? Thanks
If the table definition has the timestamp column default set to CURRENT_TIMESTAMP, you actually don't have to do anything at all. Otherwise, NOW() and CURRENT_TIMESTAMP will work, as in:
INSERT INTO t1 (timestamp_column) VALUES (NOW());
There is a difference between CURRENT_TIMESTAMP and NOW() but it's probably too small to matter to you.
phpMyAdmin seems like it has CURRENT_TIMESTAMP as an option when creating a new column.
INSERT INTO `Lines` (`date`) VALUES (NOW());
Depending on your requirement, you can also do this
$date=date('d.m.y h:i:s');
And then insert $date. I mean if you only want to view the date & time. Otherwise i also recommend time().
Otherwise... if you're using an integer, as is common in PHP, just use time() and insert it's value the same way you inserted the other variables, or use MySQL's UNIX_TIMESTAMP()
The best way to store timestamps is as UNIX timestamp integers in GMT / UTC. This allows you to always know the exact time no matter where you, your server, or your users are located. A bonus is that you can allow your users to set their timezone and display times meaningful to them.
$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ('" . time() . "')";
or
$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ( UNIX_TIMESTAMP() )";
Be careful if you choose to use NOW() or CURRENT_TIMESTAMP as they are managed by the database server and it's settings. If the server your site is hosted on is in one time zone and you move to another host in another timezone all of your timestamps will be incorrect. Using Unix integers will add a little extra effort wherever you application deals with times but it gives you the most accuracy and the most flexibility.

MySQL Insert not working with Date column

I am having an issue with a simple insert query into a table.
I have this PHP Code
$T_MEMBER = "INSERT INTO T_MEMBER (MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE) VALUES ('$memberID','$last','$first','$birthdate')";
mysql_query($T_MEMBER) or die(mysql_error());
Here are a few examples of what the query looks like if i echo it:
INSERT INTO T_MEMBER
(MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE)
VALUES
('2007','Hayes','Karin','1958-30-10')
INSERT INTO T_MEMBER
(MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE)
VALUES
('2020','Long','Peggy','1968-29-5')
INSERT INTO T_MEMBER
(MEMBER_IDENTIFIER,LAST_NAME,FIRST_NAME,BIRTH_DATE)
VALUES
('2021','Torres','Diane','1968-30-8')
BIRTH_DATE is a date type column.
The problem is, after i do any of these queries, the date shows up as 000-00-00!!!! I have been wracking my brain and i cannot seem to find the issue.
Thanks,
Ian
The date needs to be in YYYY-MM-DD format. Yours is in YYYY-DD-M(thanks juliano) format by the way.
So instead of 1958-29-05, use 1968-05-29
You might also want to consider passing in the date as a variable, and first formatting it using mktime() and date().

Categories