date() question.

Discussion in 'PHP' started by adamjblakey, Apr 18, 2007.

  1. #1
    Hi,

    Right what i am trying to do is a process that adds the date to a table in a database when an item has been added.

    What i am struggling with though is i need the listing not to show if the date in the database is more than 2 weeks old.

    I would assume i have to do an if formula on the output side which checks if the date is within range but don't know how.

    Any suggestions?

    Cheers,
    Adam
     
    adamjblakey, Apr 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    How is the date stored? If UNIX timestamp in an INT field, do something like this:


    
    "SELECT * FROM foo WHERE timestamp > " . strtotime('-2 Weeks')
    
    PHP:
     
    nico_swd, Apr 18, 2007 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    I am adding the date via this method: $date = date("m.d.y");

    Then adding $date to a field in the table called date.
     
    adamjblakey, Apr 18, 2007 IP
  4. Chamaro Zwinkels

    Chamaro Zwinkels Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    OR:

    SELECT * FROM table WHERE time > time()-3600*24*14

    Something like that (I think it is not 100% good (A))
     
    Chamaro Zwinkels, Apr 18, 2007 IP
  5. Chamaro Zwinkels

    Chamaro Zwinkels Peon

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    3600*24*14

    This = 14 days...
    time() is time now...

    This is better then date("m.d.y"); I think for you...
     
    Chamaro Zwinkels, Apr 18, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    post the code you're using currently .....
     
    krakjoe, Apr 18, 2007 IP
  7. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #7
    What i am doing currently is:

    
    $date = date("m.d.y");
    		
     mysql_query("INSERT INTO `table` (date) VALUES ('" . $date . "')") or trigger_error(mysql_error(),E_USER_ERROR); 
    Code (markup):
    This above is adding details to a table along with other details i have removed.

    Then i want to display these details on e.g. products.php page

    But i don't want an entry to display if it is more than 2 weeks old.

    I have no code on the display page yet as i did not know what to put.

    Cheers,
    Adam
     
    adamjblakey, Apr 18, 2007 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    
    <?php
    if( !( $result = mysql_query( "SELECT * FROM `table`" )) ):
    	trigger_error( mysql_error( ), E_USER_ERROR );
    else:
    	while( $assoc = mysql_fetch_assoc( $result ) ):
    	
    	if( strtotime( $assoc['date'] ) < time( ) - ( 3600*24*14 ) ):
    	/*
    		VALID RESULTS DISPLAYED HERE
    	*/
    	echo "<pre>";
    	print_r( $assoc );
    	echo "</pre>";
    	endif;
    	
    	endwhile;
    endif;
    ?>
    
    PHP:
    Give that a go, however I would have a think about how you're storing dates, the reason being once you have a load of products in that table, the overhead on queries will b huge as you have to select everything
     
    krakjoe, Apr 18, 2007 IP
  9. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I agree with krakjoe: MySQL can store dates as a native data type (with MySQL functions that can convert to and from UNIX timestamps), or just use ints to store UNIX timestamps.

    Use the correct data types and your problems will be a lot easier to control.
     
    TwistMyArm, Apr 18, 2007 IP
  10. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Thanks for that everyone, i think i am going to try as you suggested and get MySQL to store the date. I will let you know how that goes.
     
    adamjblakey, Apr 19, 2007 IP