Check if the year in line = is or is greater than 2017

I have a lot of posts and want to add a special to the ones I post this year.

offer name - July 20, 2015
offer name - July 20, 2016
offer name - July 20, 2017
...

      

I'm doing it:

$a = "offer name - July 20, 2000";
if (preg_match('/2017/',$a)) {
    echo 'offer';
}

      

running for 2017, but I want to add suggestions for posts from 2017 and beyond ... 18, 19. any ideas?

+3


source to share


5 answers


This option iterates over the inputs and filters by year:

<?php
$input = [
  'offer name - July 20, 2015',
  'offer name - July 20, 2016',
  'offer name - July 20, 2017'
];
$output = [];
array_walk($input, function($entry) use (&$output){
  if (   preg_match('| - \w+\s+\d+,\s+(\d+)$|', $entry, $tokens)
      && intval($tokens[1]) >= 2017 ) {
    $output[] = $entry;
  }
});
print_r($output);

      

This is a slightly more compact option:



<?php
$input = [
  'offer name - July 20, 2015',
  'offer name - July 20, 2016',
  'offer name - July 20, 2017'
];
$output = array_filter($input, function($entry){
  return
         preg_match('| - \w+\s+\d+,\s+(\d+)$|', $entry, $tokens)
      && (intval($tokens[1]) >= 2017);
});
print_r($output);

      

The output is obviously equal to:

Array
(
    [0] => offer name - July 20, 2017
)

      

+3


source


you can use something like this

function isYearAllowed($date, array $allowedYears)
{
    try {
        $date = new \DateTime($date);
    } catch (\Exception $e) {
        return false;
    }
    if(in_array($date->format("Y"), $allowedYears)){
        return true;
    }else{
        return false;
    }

}

      

Usage example



$allowedYears = ['2017','2016'];
if(isYearAllowed("2016-12-31", $allowedYears)){
    echo "offer";
}else{
    echo "don't offer";
}
exit;

      

live demo ( https://eval.in/835914 )

+2


source


You can use a date function to extract the year from a date string and then directly compare it to 2017

as the value it must be equal to or greater.

This function provides basic functionality whereas it returns TRUE

if the year is greater than or equal to 2017

, otherwise it returnsFALSE

function validate_year($in_string_date){

  // Particular to the case in this question, format date for date() function    
  $in_string_date = str_replace(',','',$in_string_date);    
  $in_string_date = str_replace(' ','-',$in_string_date);

  $valid_year = FALSE;

  // Get the year component from the date passed into the function.
  $year_in_date = date('Y',strtotime($in_string_date));

  /* Check if the year component extracted from the date passed
     to the function is greater or ewual to 2017 */
  if($year_in_date >= 2017){
    $valid_year = TRUE;
  }

  return $valid_year;
}

      

+1


source


There might be an easier way to do this, but until you find one, you can try doing a while loop.

1) Set the variable to the original year, you can do it statically or by the season function in php date("Y")

.

2)

while('your variable'){ //will always be true
             if (preg_match('/'.'your variable'.'/')) //if there is an offer for this year
             {
                 echo 'offer';
                 'your variable'++;
             }
             else
             {
               break;
             }
        }

      

This may not be what you are looking for, but it should work.

+1


source


You can use the functions substr

and intval

to achieve the same.

Below is the working code. You can see how it works here :

<?php
$val = array("offer name - July 20, 2000", "offer name - July 20, 2017","offer name - July 20, 2018");
foreach($val as $a)
{
    $b = substr($a,strlen($a)-4, 4);
    if(intval($b)>= 2017)
    {
        echo "Offer";
    }
    else
    {
        echo "No Offer";
    }
    echo "\n";
}
?>

      

+1


source







All Articles