SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    England
    Posts
    540
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Working with DateTime?

    Hi all,

    Im using PHP5 with MySQL, and have created a table with a DateTime field.

    Firstly, how do i insert the current date/time into it?

    INSERT INTO MyTable (RegistrationDate) VALUES (NOW()) ?

    Secondly, how can I then query the table, and check between two date ranges, e.g.

    SELECT Blarp, Blarp2 FROM MyTable WHERE Registration IS AFTER StartDate AND Registration IS BEFORE EndDate

    I've checked the documentation and can't find any good examples, any help please?

    Thanks,
    Matt.

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    England
    Posts
    540
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should probably add that the StartDate and EndDate filters are optional, so it needs to work if one of them isn't there.

    Thanks,
    Matt.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    yes, use NOW() for current datetime

    as for the range test, change

    ... WHERE Registration IS AFTER StartDate AND Registration IS BEFORE EndDate

    to

    ... WHERE Registration >= StartDate AND Registration <= EndDate
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    England
    Posts
    540
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much rudy, hadn't realised it was that simple!


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
  •