SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2000
    Location
    Chico, Ca
    Posts
    1,125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comparing DateTime in MySQL

    I don't understand why this is not working.

    PHP Code:
     function countHits()
         {
             global 
    $wpdb;
             
    $query "SELECT count(*) FROM ".$this->Table." WHERE time = 2005-02-26";
                     
             return 
    $this->wpdb->get_var($query);
         } 
    I also tried using MySQL function 'CURDATE()'. The value that keeps getting returned is '0'.

    I am trying to return rows from a specific date.

    Thanks,

    Chuck
    "Happiness doesn't find you, you find happiness" -- Unknown
    www.chuckknows.com

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    WHERE time = 2005-02-26

    requires quotes

    otherwise, 2005-02-26 is 2005 minus 02 minus 26, which equals 1997, which isn't a valid datetime
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2000
    Location
    Chico, Ca
    Posts
    1,125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    WHERE time = 2005-02-26

    requires quotes

    otherwise, 2005-02-26 is 2005 minus 02 minus 26, which equals 1997, which isn't a valid datetime
    opps..

    I still can't get it to work. I am trying to select all the records from today, but I keep getting a count of '0'.

    Here is what the statment looks like so far..

    Code:
     SELECT  * 
     FROM b2stats
     WHERE time >= {fncurdate(  )}
    "Happiness doesn't find you, you find happiness" -- Unknown
    www.chuckknows.com

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    three things i can see wrong with that query

    1. time is a reserved word and needs backticks
    2. curly braces are not allowed
    3. no such function as fncurdate

    try this query --
    Code:
    select  * 
      from b2stats
     where `time` >= current_date
    make sure you test your queries outside of php first, okay?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2000
    Location
    Chico, Ca
    Posts
    1,125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937
    three things i can see wrong with that query

    1. time is a reserved word and needs backticks
    2. curly braces are not allowed
    3. no such function as fncurdate

    try this query --
    Code:
    select  * 
       from b2stats
      where `time` >= current_date
    make sure you test your queries outside of php first, okay?
    Thanks!

    Here is what I am doing: http://www.cconway.com/wp-advanced-stats.php

    Thanks again,

    Chuck
    "Happiness doesn't find you, you find happiness" -- Unknown
    www.chuckknows.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •