Jump to content

PHP question about using dates with if statements


jdurden

Recommended Posts

This should be simple but I cant figure it out. Basically, I just need to only display titles that have thumbnails, and it should always show titles with dates from the next two months. Therefore, since it is October, it should show the October and November titles, if it's November, it shows Nov and Dec, etc. The part of the code i need help with is at the bottom.

 

[syntax=php]<?php $width = 940;

  $height = 277;

 

  $classtext = '';

  $titletext = get_the_title();

 

  $date = print(Date("l F d, Y"));

 

  $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false);

  $thumb = $thumbnail["thumb"]; ?>

 

                <?php // if there's a thumbnail

if($thumb != '' && $date == ? ) { ?>[/syntax]

Link to comment
Share on other sites

OK, looking at your code a little closer I see there is no date returned only $thumbnail["thumb"], which I assume to be the path to the thumbnail. In that case you could define variables for this month and next month, then get the value for the file using filemtime() and do your comparison.

 

Also, you say you are doing this for multiple thumbnail images. So, you must be running some of this code in a loop - which you didn't provide. Just make sure you put anything outside the loop that doesn't need to be in the loop. For example all the variable assignments I would assume should be outside the loop.

 

    ### Outside the loop ###
    $width = 940;
    $height = 277;
    $classtext = '';
    $titletext = get_the_title();
    $date = print(Date("l F d, Y"));

    //Defne this and next month
    $thisMonth = date('Y-m');
    $nextMonth = date('Y-m', strtotime('next month', strtotime("($thisMonth)-1")));

    ### Inside the loop ###
    $thumbnail = get_thumbnail($width, $height, $classtext, $titletext, $titletext, false);
    $thumb = $thumbnail["thumb"];

    //Get Year-Month of the thumbnail file
    $thumbMonth =  date('Y-m', filemtime($thumbnail["thumb"]));

    if($thumb != '' && ($thumbMonth == $thisMonth || $thumbMonth == $nextMonth) ) {
    

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.