Q:

Sort glob array starting from current month

Im working on a glob gallery, it shows evens (shown as picture) for each month.
This is my code to do so:
$folderGlob = glob("gallery/events/2015/*/");
foreach($folderGlob as $folder){
    $fileglob = glob($folder . "*.{jpg,png}", GLOB_BRACE);
    foreach($fileGlob as $file){
        // lets say i dont have images, but i just echo $file
        echo $file;
    }
}

the output will be like this:
gallery/events/2015/01/01.jpg
gallery/events/2015/02/04.jpg
gallery/events/2015/03/09.jpg
gallery/events/2015/04/21.jpg
gallery/events/2015/05/04.jpg
gallery/events/2015/06/09.jpg
gallery/events/2015/07/21.jpg
... etc

as you can see my map structure works like a date, with the file name as day and the map name as month. now i need to sort this so that the current mont gets shown first.
here the example what i want:
gallery/events/2015/03/09.jpg //current month
gallery/events/2015/04/21.jpg
gallery/events/2015/05/04.jpg
gallery/events/2015/06/09.jpg
gallery/events/2015/07/21.jpg
... 
gallery/events/2015/12/02.jpg //lastmonth
gallery/events/2015/01/01.jpg 
gallery/events/2015/02/04.jpg

any tips or tricks?
EDIT:
i now use this:
$currentMonth = date("m");
$month = intval($currentMonth);

foreach ($folderGlob as $folder) {
    $folderMonth = intval(basename($folder));
    if($folderMonth < $month){
        continue;
    } else {
        print $folderMonth . '<br>';
    }
}

foreach ($folderGlob as $folder) {
    $folderMonth = intval(basename($folder));
    if($folderMonth >= $month){
        continue;
    } else {
        print $folderMonth . '<br>';
    }
}

output is :
gallery/2015/03/
gallery/2015/04/
gallery/2015/05/
gallery/2015/06/
gallery/2015/07/
gallery/2015/08/
gallery/2015/09/
gallery/2015/10/
gallery/2015/11/
gallery/2015/12/
gallery/2015/01/
gallery/2015/02/

A:

Use a custom sort function:
$array = array('gallery/events/2015/01/01.jpg',
'gallery/events/2015/02/04.jpg',
'gallery/events/2015/05/04.jpg',
'gallery/events/2015/04/21.jpg',
'gallery/events/2015/06/09.jpg',
'gallery/events/2015/03/09.jpg',
'gallery/events/2015/07/21.jpg');

usort($array, function($a, $b) {

    $currentmonth = date('n');
    preg_match('#^gallery/events/(\d+)/(\d+)/(\d+)\.jpg$#', $a, $matchesa);
    preg_match('#^gallery/events/(\d+)/(\d+)/(\d+)\.jpg$#', $b, $matchesb);

    $timea = mktime(0, 0, 0, $matchesa[2], $matchesa[3], $matchesa[1]);
    $timeb = mktime(0, 0, 0, $matchesb[2], $matchesb[3], $matchesb[1]);

    $montha = (int)$matchesa[2];
    $monthb = (int)$matchesb[2];

    if ( 
        ($montha > $currentmonth && $monthb > $currentmonth) ||
        ($montha < $currentmonth && $monthb < $currentmonth) ||
        ($montha == $currentmonth && $monthb == $currentmonth) )
        {
            if ( $timea < $timeb ) return -1;
            else if ( $timea > $timeb ) return 1;
            else return 0;
        }
        else
        {
            if ( $montha < $currentmonth ) return 1;
            else if ( $montha > $currentmonth ) return -1;
            else return 0;
        }
});

print_r($array);

Here is a working test.

