1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Is this possible? Show - NEW if by date / time under 24hrs

Discussion in 'PHP' started by phplduser, Mar 6, 2020.

  1. #1
    Hi,

    On my home page i have the latest 5 listings displayed, what i wondered is if a listing is under say 24hrs it could display 'New' and if over 24 hours the 'New' is not displayed? Here is some of the code i'm using -

        <?php
        $images=explode(",",$listing["images"]);
        if($listing["images"]!=""&&file_exists("thumbnails/".$images[0].".jpg"))
        {
            $images = explode(",",$listing["images"]);
           
            echo "<a href=\"".$strLink."\"><img align=\"left\" src=\"thumbnails/".$images[0].".jpg\" width=\"50\" alt=\"".stripslashes(strip_tags($listing["title"]))."\" class=\"img-shadow img-right-margin\" /></a>";
        }
        ?>
       
        <h4 class="no-top-margin"><a href="<?php echo $strLink;?>">
            <?php echo stripslashes(strip_tags($listing["title"]));?>
        </a></h4>
        <span class="sub-text">
        <?php echo $this->text_words(stripslashes(strip_tags($listing["description"])),10);?>
        </span>
       
        <div class="clear"></div>
       
        <div class="smooth-separator margin_10"></div>
    <?php
    }
    ?>
    <?php
    if(!isset($_REQUEST["mod"])&&(!isset($_REQUEST["page"])||(isset($_REQUEST["page"])&&$_REQUEST["page"]=="en_Home"))&&$this->db->num_rows($SearchTable)>0)
    {
    ?>
        <div class="text-center">
            <a class="underline-link" href="<?php echo $this->mod_link("latest_listings");?>"><?php echo $M_SEE_MORE;?></a>
        </div>
    <?php
    }
    ?>
    Code (markup):
    Best Regards
    J
    SEMrush
     
    phplduser, Mar 6, 2020 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    25,776
    Likes Received:
    3,648
    Best Answers:
    103
    Trophy Points:
    665
    #2
    OMG that's ugly code. I feel quite faint just looking at it.

    Try this

    
    <?php
        $images=explode(",",$listing["images"]);
        $title = stripslashes(strip_tags($listing["title"]));
      
        if(count($listing["images"]) && file_exists("thumbnails/".$images[0].".jpg")){
            echo "<a href='{$strLink}' title=\"{$title}\">
            <img align='left' src='thumbnails/{$images[0]}.jpg' width='50' alt=\"{$title}\" class='img-shadow img-right-margin' />
            </a>";
        }
        ?>
      
        <h4 class="no-top-margin"><a href="<?php echo $strLink;?>"><?php echo $title;?></a></h4>
        <span class="sub-text">
        <?php
        //why isn't $this->text_words also doing the stripslashes and strip_tags?
         echo $this->text_words(stripslashes(strip_tags($listing["description"])),10);
        $listing_date = strtotime($listing['listing_date']);
        $yesterday = strtotime('yesterday 00:00:00');
        if ($listing_date > $yesterday){
            echo "<span>I'm new</span>";
        }
        ?>
        </span>
      
        <div class="clear"></div>
       <!-- why isn't smooth-separator also doing the clear? -->
        <div class="smooth-separator margin_10"></div>
    <?php
    }
    ?>
    <?php
    $mod = filter_input(INPUT_GET, 'mod', FILTER_SANITIZE_STRING);
    $page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING);
    
    
    if(is_null($mod) && (is_null($page)|| $page == "en_Home") && $this->db->num_rows($SearchTable)>0):
    ?>
        <div class="text-center">
            <a class="underline-link" href="<?php echo $this->mod_link("latest_listings");?>"><?php echo $M_SEE_MORE;?></a>
        </div>
    <?php
    // where code is really messy and it's easy to lose the closing bracket I like to use this style of if/endif
    endif;
    ?>
    
    Code (php):
     
    Last edited: Mar 17, 2020
    sarahk, Mar 16, 2020 IP
    phplduser likes this.
  3. phplduser

    phplduser Member

    Messages:
    276
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    33
    #3
    Hi SarahK,

    I done a test listing and it's not showing as 'New', Looking at the DB listing_date (if this makes any difference) for some reason uses 10 digits that don't even make sense as regards to say - day, month, year & time.

    Best Regards
     
    phplduser, Mar 17, 2020 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    25,776
    Likes Received:
    3,648
    Best Answers:
    103
    Trophy Points:
    665
    #4
    It might be storing a time()

    what do you get if with this

    date('d M Y', $row['listing_date'])
    Code (php):
    if that is sensible then my code from line 17 above becomes
    
    $listing_date = $listing['listing_date'];
    $yesterday = strtotime('yesterday 00:00:00');
    if ($listing_date > $yesterday){
    Code (php):
     
    sarahk, Mar 17, 2020 IP
  5. phplduser

    phplduser Member

    Messages:
    276
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    33
    #5
    Hi Sarahk,

    Still not showing, I've had a look around the code and in another file it displays the listing time and uses this code.

    <?php echo time_since($listing["date"])." ".$M_AGO;?>
    Code (markup):
    Not sure if this helps in anyway.

    Best Regards
     
    phplduser, Mar 17, 2020 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    25,776
    Likes Received:
    3,648
    Best Answers:
    103
    Trophy Points:
    665
    #6
    I think that might be a function in that directory but you should be able to use it for your purposes if that $M_AGE is talking about minutes and not months.

    $time_diff = time_since($listing["date"]);
    if ($time_diff < (60*24)) {
       echo 'new';
    }
    Code (php):
     
    sarahk, Mar 17, 2020 IP
    phplduser likes this.
  7. JEET

    JEET Notable Member

    Messages:
    2,899
    Likes Received:
    244
    Best Answers:
    9
    Trophy Points:
    215
    #7
    I think your database stores a unix timestamp for date. A 10 digit number, something like: 1584515485

    To display "New Listing", modify your code like this:

    <h4 class="no-top-margin"><a href="<?php echo $strLink;?>">
    <?php echo stripslashes(strip_tags($listing["title"]));?>
    </a>
    <?php
    if( isset($listing['date']) &&
    (time()-$listing['date'])<(3600*24*1)
    ){ echo "New Listing"; }
    ?>
    </h4>
     
    JEET, Mar 18, 2020 IP
    phplduser likes this.
  8. phplduser

    phplduser Member

    Messages:
    276
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    33
    #8
    Hi JEET,

    Just had to change a little bit of your code and it works. Had to edit it to 24 hrs in seconds.

    Many thanks to you and sarahk.
     
    phplduser, Mar 18, 2020 IP
    JEET and sarahk like this.
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,171
    Likes Received:
    1,725
    Best Answers:
    239
    Trophy Points:
    515
    #9
    Sadly though, whilst @JEET has the right answer for the date, as @sarahk said it's a wreck.

    1) Stop opening/closing PHP willy-nilly, you're just making yourself work harder by having less clear code.

    2) Stop using double quotes for your strings, you're just making yourself work harder by having to escape doubles or use the "wrong" quotes client-side.

    3) Stop using multiple echo to do one echo's job.

    4) this is 2020 not 1997, stop using presentational classes like "no-top-border"., it defeats the point of CSS' existence at which point you might as well go back to writing HTML 3.2.

    5) Are you REALLY sure this is down to H4 depth structurally? Much less what is the H4 a heading OF?

    6) This is 2020 not 2003, what's with nonsense like the clearing DIV and separator DIV? Generally those are an indication that whoever's writing the HTML shouldn't be.

    7) Don't use string addition on echo when you don't have to. It slows things down and uses more memory than comma delimited output.

    8) Not sure what "$this->text_words" does, but the garbage being thrown into that function -- and possibly the function itself -- is, well... garbage. It's called htmlspecialchars, use it.

    If this is representative of the code driving the site, both front and back end need to be pitched in the trash and started over from scratch.
     
    deathshadow, Mar 18, 2020 IP
    JEET likes this.
  10. phplduser

    phplduser Member

    Messages:
    276
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    33
    #10
    @deathshadow

    I'm not a coder, i only understand a limited amount of code that's why i come here to ask for help, if i had the money i would love to get a coder to write it from scratch so all i can do is 'make do' with what i have i'm afraid but thank you for your insights.

    Best Regards
     
    phplduser, Mar 18, 2020 IP
    sarahk likes this.
  11. JEET

    JEET Notable Member

    Messages:
    2,899
    Likes Received:
    244
    Best Answers:
    9
    Trophy Points:
    215
    #11
    No problem, happy to help :)
     
    JEET, Mar 18, 2020 IP
    phplduser likes this.