I have a array of dates like this

$dates = ('2014-01-21','2014-03-11','2013-01-21','2014-03-11','2013-07-21','2012-01-21');

I want to get array values where year is 2014. I mean when i change value to 2014 it will return value from array which contain year 2014 only. In this case it will return '2014-01-21','2014-03-11' to me. Is there a way to do this.

Recommended Answers

All 4 Replies

?

Member Avatar for diafol
$dates = array('2014-01-21','2014-03-11','2013-01-21','2014-03-11','2013-07-21','2012-01-21');
$year = 2014;
$filteredItems = array_unique(array_filter($dates, function($elem) use($year){
                     return substr($elem,0,4) == $year;
                 }));

print_r( $filteredItems);
Member Avatar for diafol

It is for me. Which versiĆ³n of php? You need 5.3 or later. You shouldn't be using anything earlier anyway.

Here's a demo using closures (anonymous functions) with array_filter:

http://demos.diafol.org/filter-arrays-with-parameters.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.