Results 1 to 6 of 6

Thread: Date/Time problem

  1. #1

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Date/Time problem

    Hello everybody,

    Firstly, I have a datetime field in mySQL database and I want to insert system datetime whenever record is inserted into the table. If I use time() function, it returns something "1142866431". I need to save it in such a way that I can track the minutes and hour.

    Secondly, I need to get all records from the same table that are past 24 hours or more.

    Thanks.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Date/Time problem

    to get all records within the last 24 hours use something similar to this

    Code:
    SELECT * from table WHERE DATE_SUB(now(), interval 24 hour) >= stored_time
    and to format the date stored in the table
    Code:
    DATE_FORMAT(stored_time,"%h:%i%p")
    All the details can be found here:
    http://dev.mysql.com/doc/refman/5.0/...functions.html

  3. #3

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Date/Time problem

    Thanks.

    I do not want to format stored data, I just want to format it at the time of adding. Moreover, for getting records of past 24 hours, I think we also need to track date as well, it may happen that a record was saved at 10:00 PM hence, the next 24 hour will be of next day.

    What do you suggest ?

  4. #4
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Date/Time problem

    If working with dates use John's method as it's a pain (and doesn't always return corerct results) when you save dates as 10.00pm, 09.00am etc...

  5. #5
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Date/Time problem

    if you want to format it at time of adding then as long as you want the current time you can use
    Code:
    INSERT INTO table (date_value) VALUES (DATE_FORMAT(NOW(),"%h:%i%p"))
    What do you mean by track the date? Do you want to select records from a certain time range, not sure what your looking for.

    [Edit]
    <reminder>Must learn to read what was posted before......</reminder>

  6. #6

    Thread Starter
    Frenzied Member usamaalam's Avatar
    Join Date
    Nov 2002
    Location
    Karachi
    Posts
    1,308

    Re: Date/Time problem

    I have a cron scheduler that runs every 3 minutes and insert some records in the database with current time. The same page then deletes the records that are past 24 hours.

    Tracking the date means if record is inserted at 10:00 PM, then next 24 hours will be on the next day date, I also need to maintain this that code should successfully delete records that are 24 hours old.

    Thanks a lot.

Posting Permissions

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



Click Here to Expand Forum to Full Width