How do I check with preg_match if string contains year which is between 1950 and 2010 ?
example:
$string = "I was born in 1986 year.";
How do I check with preg_match if string contains year which is between 1950 and 2010 ?
example:
$string = "I was born in 1986 year.";
Assuming you have a timestamp:
$date = getdate($timestamp);
if ($date['year'] >= 1950 && $date['year'] <= 2010)
return 'good';
$date = getdate( $timestamp );
if( (int)$date['year'] >= 1950 && (int)$date['year'] <= date('Y') ) {
return 'good';
}
:P
if( preg_match( '/^(19[5-9]{1}[0-9]{1}|20(0[0-9]{1}|10))$/', $i ) ) {}
// tested - works. 1950-2010
If the string is already formatted in a way that can be consumed by getdate(), the other answers would be the best solution.
If the string is just some random text which might or might not contain a date, you'd need to use a regex to find those numbers.
/(19[5-9][0-9]|20(0[0-9]|10))/
Of course, you have no guarantee that the numbers matched this way is actually a year. It could be 2005 pounds of steel or 1976 miles of highway.
It's not ok like that: 19[5-9]{1}[0-9]{1}|20(0[0-9]{1}|10)
Why?
Because I get current year from date('Y'): $current_year = date('Y'); $current_work_year = substr($current_year, -2);
19[5-9]{1}[0-9]{1}|20(0[0-9]{1}|$current_work_year)