Results 1 to 3 of 3

Thread: PHP MySQL dates

  1. #1
    Junior Member
    Join Date
    Apr 2006
    Posts
    42

    PHP MySQL dates

    OK, I have a webpage that is running php and mysql. one field is a date field entered as M-D-Year. I want to place an alert about items less then 7 days old. I am not sure what type of calculation to do to get this.....

    Any thoughts?

  2. #2
    Elite Member TonyT's Avatar
    Join Date
    Jan 2000
    Location
    Fairfax, VA
    Posts
    10,336
    How are you storing the date in mysql?
    You could do what you want easily combining php & javascript.
    Grab the sql result (date) & put make it into a varialbe and check to see if it's less than 7.

    example w/out javascript

    <?

    $sql_date = mysql_query("SELECT item, TO_DAYS(now())-TO_DAYS(date)as date_diff FROM table WHERE id='$id'") or die (mysql_error());
    while(list($item, $date_diff)=mysql_fetch_array($sql_date)){
    if($date_diff < 7){
    $new = "y";
    }
    echo "Item: $item";
    if($new == "y"){
    echo " <font color="red">New!!</font>";
    }
    mysql_free_result($sql_date);
    unset($new);
    }

    ?>

    You could also do an if-else:

    if($date_diff < 7){
    echo "<script language=\"javascript\" type=\"text/javascript\">alert('Item is new!')\;";
    </script>

    }
    else {desired code here
    }
    Last edited by TonyT; 05-01-07 at 07:40 AM.
    No one has any right to force data on you
    and command you to believe it or else.
    If it is not true for you, it isn't true.

    LRH

  3. #3
    no. no, No. NO, NO... NO! stevebakh's Avatar
    Join Date
    Aug 2002
    Posts
    3,228
    Quote Originally Posted by TonyT View Post
    How are you storing the date in mysql?
    You could do what you want easily combining php & javascript.
    Grab the sql result (date) & put make it into a varialbe and check to see if it's less than 7.
    In fact, it would be much better to do any calculation that you can, within SQL. Rather than doing comparisons in your application code, do them all in SQL as it's much faster.

    You can do date comparisons in SQL by treating them like strings. In fact, everything, as far as queries go, are treated like strings.

    If you store your dates in standard ISO format, for example, like YYYY-mm-dd HH:mm:ss, or even just YYYY-mm-dd, then you could generate a date set for 7 days previous and compare against that.

    PHP example:

    to create a date 7 days prior to now in standard ISO format.

    Code:
    $now = date("Y-m-d H:i:s");
    $sevendaysago = date($now, mktime(0, 0, 0, 0, -7, 0));
    Then in your SQL, you pull all records where their creation date is greater than or equal to the date held in $sevendaysago.

    Note, you can also use the DATE_FORMAT() function inside your MySQL query to change the format of the date, if you're using a different formatting.

Similar Threads

  1. Passing Javascripts values to PHP without redirecting
    By BlueJade27 in forum Programming Forum
    Replies: 3
    Last Post: 12-03-06, 05:41 PM
  2. Looking for help with PHP....
    By Chapaev in forum Programming Forum
    Replies: 2
    Last Post: 03-02-06, 02:15 PM
  3. PHP problems....
    By BlindPilot in forum Programming Forum
    Replies: 6
    Last Post: 06-20-05, 03:27 AM
  4. PHP Editor: beta-tests
    By Luckasoft in forum Programming Forum
    Replies: 0
    Last Post: 02-23-05, 03:16 AM
  5. MySQL server configuration questions
    By Blisster in forum General Discussion Board
    Replies: 14
    Last Post: 02-10-05, 10:43 PM

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
  •