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]

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) ) {
    

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.