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 3 of 3
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Comparing dates with sql and dateAdd

    Hi,

    I found a tutorial here which basically is a php version of asp and vb's dateAdd, and have been using it on a page of mine.

    The idea is that the code will look at my database of items and only show those that have been added in the last 30 days, with the date stored in the db.

    The code I have is:

    PHP Code:
    $sql 'SELECT * FROM item WHERE date > "' .dateadd("d",-30getdate()). '"'
    PHP Code:
    function DateAdd($interval$number$date) {

        
    $date_time_array getdate($date);
        
    $hours $date_time_array['hours'];
        
    $minutes $date_time_array['minutes'];
        
    $seconds $date_time_array['seconds'];
        
    $month $date_time_array['mon'];
        
    $day $date_time_array['mday'];
        
    $year $date_time_array['year'];

        switch (
    $interval) {

            case 
    'yyyy':
                
    $year+=$number;
                break;
            case 
    'q':
                
    $year+=($number*3);
                break;
            case 
    'm':
                
    $month+=$number;
                break;
            case 
    'y':
            case 
    'd':
            case 
    'w':
                
    $day+=$number;
                break;
            case 
    'ww':
                
    $day+=($number*7);
                break;
            case 
    'h':
                
    $hours+=$number;
                break
            case 
    'n':
                
    $minutes+=$number;
                break;
            case 
    's':
                
    $seconds+=$number;
                break;
        }
           
    $timestampmktime($hours,$minutes,$seconds,$month,$day,$year);
        return 
    $timestamp;

    The problem is that all of the items are showing not just the ones from the last 30 days. I've played around with it but nothing has made it work and most broke it more.

    Any clues why it isn't working, or any alternative suggestions would be much appreciated!

    Thanks!

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,039
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Echo out your query sting $sql to see what it contains.

    You are passing a getdate() result in the function call that is then used in a getdate() inside of the function, instead of a "date"

    Edit: You can use mysql built in date and time functions directly in your query to accomplish the same thing without needing any of that PHP function code.
    Last edited by CFMaBiSmAd; 07-28-2007 at 05:35 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    Edit: You can use mysql built in date and time functions directly in your query to accomplish the same thing without needing any of that PHP function code.
    Didn't see your edit... Also, seems as if you should use mysql's DATE_SUB() instead of DATE_ADD().
    http://dev.mysql.com/doc/refman/5.0/...ction_date-add
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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