Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Comparing UNIX time within a MySQL query.

    Hi,

    I have a table where the date is stored as UNIXTIME. I would now want to search the database for specific dates but the problem is some of the dates are stored in the database with hours and minutes. For example the following dates are saved in the Database:

    10-11-2010 04:00 PM stored as (1291046400)
    When I want to search for the date 10-11-2010 I want it to show the above date, is there any built-in MySQL functions that I can use to achieve this?

    Help is much appreciated. Thanks in advance

  • #2
    New Coder
    Join Date
    Nov 2010
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post
    You can call strtotime($date_string) to get the UNIX timestamp of a date string, like:

    PHP Code:
    $date_string '10-11-2010 04:00';
    $date_timestamp strtotime($date_string);
    $query "SELECT * FROM myTable WHERE timestamp='{$date_timestamp}'"

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts
    I know strtotime(), the problem is I don't want to pass the hours/minutes and just want to search by date. Thanks

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    PHP Code:
    $date_string '10-11-2010 04:00';
    $date_only   date('Ymd',strtotime($date_string));
    $query 'SELECT * FROM `myTable` WHERE DATE_FORMAT(FROM_UNIXTIME(`timestamp`),"%Y%m%d") = ' $date_only
    Last edited by djm0219; 11-28-2010 at 02:10 PM. Reason: Forgot the conversion of the Unix timestamp to MySQL format for the date_format
    Dave .... HostMonster for all of your hosting needs

  • #5
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts
    LOL, I am saying I don't have the hours and minutes, my $date_string looks like
    PHP Code:
    $date_string '10-11-2010'
    Does it make sense?

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    That isn't what you said but it shouldn't matter. strtotime will still work. Since you are ignoring the time anyway and not using it it won't matter what strtotime will use as the default time.
    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

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