Help with SQL Statement

Discussion in 'PHP' started by adamjblakey, Mar 11, 2009.

  1. #1
    Hi,

    Please can you help me with the following.

    I am running an SQL statement and want to compare on the day and not the full date. What i have done is the following:

    
    
    <?php
    $date = date("d");
    $sql = mysql_query("SELECT * FROM `customer_orders` WHERE type = '1' AND date = '$date'");
    ?>
    
    PHP:
    The date in the datebase is in the follow format e.g. yyyy-mm-dd i need a statement something like this:

    $sql = mysql_query("SELECT * FROM `customer_orders` WHERE type = '1' AND date(m) = '$date'");
    Code (markup):
    Thanks in advance.
    Adam
     
    adamjblakey, Mar 11, 2009 IP
  2. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if field 'date' is a string with yyyy-mm-dd format, then you probably need to change only first line
    $date = date("Y-m-d");
     
    cont911, Mar 11, 2009 IP
  3. adstiger

    adstiger Peon

    Messages:
    409
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    date("Y-m-d") will provide the format as Year-Month-Day
     
    adstiger, Mar 11, 2009 IP
  4. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #4
    I dont want to match up the exact date, i want to check the day e.g. 15 and if today day is 15 also then a match is made.
     
    adamjblakey, Mar 12, 2009 IP
  5. smrutik

    smrutik Guest

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hi Adam,

    If you are using MySQL database, try using follwing stmt in your query:

    day(str_to_date(OrdDt, '%m/%d/%Y' ) ) = '02'

    use str_to_date() if date field is getting stored as string, otherwise no need to use it.

    thanks,
    Smruti.
     
    smrutik, Mar 12, 2009 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Unfortunately that does not work, the date is stored in date format.

    I thought this may work also but doesn't.

    DAY(GETDATE(date)) = '02'
     
    adamjblakey, Mar 12, 2009 IP
  7. smrutik

    smrutik Guest

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hi,

    I hope you tried like this:

    mysql_query("SELECT * FROM `customer_orders` WHERE type = '1' AND day(date) = '$date'");
     
    smrutik, Mar 12, 2009 IP
  8. mighty_falcon

    mighty_falcon Member

    Messages:
    67
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    the easiest thing you can do when comparing date and timestamps is to use/cinvert them to the unix timestamp


    i cant post any links thus far but try to google unix timestamp and u will see lots of ways to use it
     
    mighty_falcon, Mar 12, 2009 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Why not just add something like this:

    $date = explode("-", $sql["date"]);

    Then you just echo $date[2] for the day part ($date[0] is the year, $date[1] is the month)

    Then you can just compare the $date[2] with whatever value you want
     
    PoPSiCLe, Mar 13, 2009 IP
  10. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    yes,

    definately you can use the explode function and store them in some variables..
    and you can make on addition functions work according to ur wish..
    example..

    if you need the months in the format:
    1,2,3,4,5,6,7,8,9
    the make one function which removes the zero from the variables..i think the
    after exploding date("Y-m-d"); ///YYYY-mm-dd//////
    $date = date("Y-m-d");
    $date = explode('-',$date);


    $date[1] will have 01,02,03,04,05,06,07,08,09...

    So you can make on function to removed the zeros or simply use a case switch to replace it..

    I hope this will work..
     
    Stylesofts, Mar 13, 2009 IP
  11. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #11
    And you could call that function... intval()!
     
    SmallPotatoes, Mar 13, 2009 IP