SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi folks,
    Just wondering, if I have a date stored in a database and I pull it out and store it in $entrydate, how do I compare it to the CURRENT date?

    Which format would be easiest? UNIX timestamps? Normal day/month?

    I basically need to check if it is more than 3 days since $entrydate.

    Thanks very much folks,

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you only need to grab the records that are more than three days old?

    Code:
    $result = mysql_query("SELECT * FROM tablename WHERE date < DATE_SUB(CURDATE(), INTERVAL 3 day);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi freddy,
    No, the records will be grabbed and stored in $entrydate, then if $entrydate is more than 3 days ago, another variable is changed.

    ie.

    $entrydate ----> from DB
    $currentdate -----> today

    if $entrydate is 3 or more days ago, set $datecheck to "YES".

    Cheers,

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are two options (both untested code). I prefer the second.

    1)
    PHP Code:
    $THREE_DAYS 60*60*24*3;

    $sql "SELECT UNIX_TIMESTAMP(entryDate)
                 FROM tableName"
    ;
    $result $mysql_query($sql);
    while (
    $row mysql_fetch_array($result)) {
       if( 
    time() > $row["entryDate"] + $THREEDAYS ) {
          
    $dateCheck true;
       }

    2) And my prefered ...
    PHP Code:
    $sql "SELECT ( CURDATE() > entryDate + INTERVAL 3 day ) AS dateCheck
                 FROM tableName"
    ;
    $result mysql_query($sql); 
    You will now have a result set with the boolean field dateCheck for each record which will be 0 (false) if the entryDate is less than three days ago and 1 (true) if it is older. IMHO, the more work you can do with your SQL statement to shape the result set into the data you want, the less mucking about you need to do with your php code.
    Last edited by freakysid; Apr 22, 2001 at 03:59.

  5. #5
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, so if I do the following:-

    PHP Code:
    if ($dateCheck == "0") {
    print 
    "This date is less than 3 days ago";
    } elseif (
    $dateCheck == "1") {
    print 
    "This date is more than 3 days ago";

    That should work?

    Just checking as I'm not able to try it out. Damn work

    Thanks,

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks good. you could even write that code like this:
    PHP Code:
    if( ! $dateCheck ) {
       
    print*"This*date*is*less*than*3*days*ago";
    } else {
       
    print*"This*date*is*more*than*3*days*ago";

    As 0 evaluates to false and 1 evaluates to true. Also as you know that the value is boolean (either 0/false or 1/true) there is no need to test two conditions, just one (ie, you can use else instead of elseif)

    Finally, better do some boundary checking to make sure what I gave you is what you want. If today is 22 April and the date in the database is 19 April then 19 +3 = 22. And 22 < 22 is false. So are you checking for records that are three days or more old, or records that are more than three days old?

  7. #7
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freakysid,
    Thanks for all your work man, you wouldn't believe how appreciated it is.

    Anyways, it would be to check if it's MORE than 3 days old. If it's exactly 3 days, it should come back false so the code is spot on.

    Thanks again!

  8. #8
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wondering,
    Is it possible to combine that SELECT statement with another one.

    Basically, I'm creating a script that'll keep a list of clients, their details, when they contacted me etc etc

    I'm going to set a 3 day limit to contact the client after they initially contacted me.

    When I look at the main page, I need the script to check whether it's 3 days since the initial contact and then popup a notification window. I have the JavaScript down to get the popup window to appear, it's just that if I have a select statement to check the date for EACH contact, in a database of 100 clients, it'll slow down.

    I select the clients in the following way:-

    PHP Code:
    $query "SELECT * from clients where current = 'YES' ORDER BY " $sort " " $direction
    Is there anyway that I can combine the statement you wrote with that one?

    Thanks,

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you are doing it the hard way to me, so you ned to grab all the data, but you also want to know which records are older than 3 days, correct?

    So use,

    Code:
    $result = mysql_query("SELECT field1, field2, etc.., IF(datefield < DATE_SUB(CURDATE(), INERTVAL 3 day),1,0) as dateCheck from tablename");
    //So now your records will hold all the info plus a 1 or 0 in the dateCheck field 1 means greater than 3 days old, 0 means not.
    while($row=mysql_fetch_array($result)) {
    if($row["dateCheck"] == 1) {
    print "Is older";
    }
    else {
    print "Is not older";
    }
    }
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what if we were comparing months?
    i dunno...

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by comparing months?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as in.. if i wanna select records that are older then 6mths?
    i dunno...

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $result = mysql_query("SELECT * FROM tablename WHERE date < DATE_SUB(CURDATE(), INTERVAL 6 month);
    Last edited by freddydoesphp; Apr 22, 2001 at 11:06.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  14. #14
    SitePoint Addict kunal's Avatar
    Join Date
    Oct 2000
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah.. thanx..

    i guess today is my being stupid night
    i dunno...


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
  •