简体   繁体   中英

check if year in string is = or bigger than 2017

I have many posts and I want to add some special offer in the ones that I post this year.

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

I'm doing this:

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

it is working for 2017, but I want to add offers in posts from 2017 and next years... 18, 19. any ideas?

This variant iterates over the input 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 somewhat more compact variant:

<?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 obviously is:

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

you can use something like that

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;
    }

}

Example of usage

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

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

You can use substr and intval function to achieve the same.

Following is working code. You can see it working 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";
}
?>

You can use the date function to extract the year from the date string and then directly compare it to 2017 as the value it should be equal to or greater than.

This function provides that basic functionality whereas it returns TRUE if the year is greater than or equal to 2017 , else it returns FALSE

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;
}

There may be an easier way to do this but until you find it you could try doing a while loop.

1) Set a variable to the starting year, you could do this static or by the year time 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;
             }
        }

It may not be what your looking for but it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM