Jump to content

[SOLVED] USING NOW() to display list of today


samona

Recommended Posts

Hi,

I have an ongoing assignment that I'm programming.  Thanks for all previous help.  Right now

I have stored some records in a database using NOW() as the value for the created field.

 

I am trying to display only records that have been created today and my query is producing incorrect results.

 

For example,

 

$sql = "Select name, task  From 'table' where created= CURDATE()";

 

It's  not displaying anything.  I noticed that the created field has (year-month-day hr-min-sec), so I'm not sure how I would query for what was created in (year-month-day) and NOT hr-min-sec.

 

Any help will be appreciated.

Link to comment
Share on other sites

I'm getting the following error:

 

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near '( created ) = CURDATE( )

LIMIT 0, 30' at line 1

 

I was reading the DATE() function in the php manual and it doesn't show a way to convernet NOW() to DATE(), so am I correct to say that MySQL is not capable of performing this conversion?

Link to comment
Share on other sites

I don't know how great this code is but it works:

 

Select * From task_user WHERE DATE_FORMAT(created, '%Y %m %d') = DATE_FORMAT(CURDATE(), '%Y %m %d')

That worked to display tasks that were created today.  I hope that's useful to someone.  If anyone knows any better way to do it, please post it.  Thanks!

Link to comment
Share on other sites

OK, to do yesterday you would write the following query

 

 

Select * From task_user WHERE DATE_FORMAT(created, '%Y %m %d') = DATE_FORMAT(CURDATE() - INTERVAL 1 DAY, '%Y %m %d')

 

 

Again, I'm new at this stuff so I don't know how optimized this code is.  If anyone knows better please post.  For the rest of us newbies this works.

 

Thanks to fenway for helping me with the idea.

Link to comment
Share on other sites

I was reading the DATE() function in the php manual and it doesn't show a way to convernet NOW() to DATE(), so am I correct to say that MySQL is not capable of performing this conversion?

This is a MySQL function, not a php function!  But it's possible there's a version issue here...

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.