Jump to content

Insert Into DB DateTime and Echo Out Only Date?


chaseman

Recommended Posts

What I'm basically trying to accomplish is, that the datetime of the user posted information is registered with the Now() function in the query, BUT that the user entered information gets echo'd out with the Date showing ONLY.

 

What I have is this:

-Just as an example-

 

INSERT INTO db (date_created) VALUES (now())

while ($row = mysqli_fetch_array($data)) {

echo '<tr><td>' . $row['date_created'] . '</td></tr>';

}
mysqli_close($dbc);

 

With this way of course I get Date and Time showcased just like it's inserted into the DB.

 

What would be a legit way of showcasing just date.

 

Thanks for help.

 

EDIT: or is there a better way than using now() to showcase the date when the user entered information was posted?

Link to comment
Share on other sites

To just get the date part of a datetime value when you select the data, use the mysql DATE() function in your query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date

 

However, since you are probably formatting the date into some format other than YYYY-MM-DD, use the mysql DATE_FORMAT() function in your query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

 

No slow parsed, tokenized, interpreted php code is needed. Doing this in your query is at least 8 times faster than using php code.

Link to comment
Share on other sites

Not sure if I understand this clearly.

 

Using the date() function in the query would also mean that the date gets inserted into the Database WITHOUT the time right? That's not what I want tho. I want it to be inserted with the time, BUT echo'd out WITHOUT the time...

 

Is there not a function where I just can take the datetime from the database and leave out the time when echo'ing out?

Link to comment
Share on other sites

  • 4 weeks later...

For every body who is wondering the same question I finally solved this problem with this solution:

while ($row2 = mysqli_fetch_array($data2)) {
echo '<table><tr><td>' . date('M d, Y', strtotime($row['created_date'])) . '</td></tr></table>';
}
mysqli_close($dbc);

 

The solution is date(' ', strtotime()), with the M d, Y you can display the time how ever you want it, BUT the date still gets as a full string inserted into the database with the posted time in the query with now(), as described in the first post.

 

I'll mark this as solved.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.