Jump to content

Find next month in sequence of dates


c_shelswell

Recommended Posts

Hi i'm trying to find out each month in an array of dates I might have about 4000 different dates in the end all i need to do is find what months are present in the array.

my array is currently:

Array ( [0] => 2006-05-18 [1] => 2006-05-21 [2] => 2006-06-23 [3] => 2006-06-27 [4] => 2006-07-17 [5] => 2006-07-17 [6] => 2006-08-01 [7] => 2006-08-01 [8] => 2006-09-14 [9] => 2006-09-17 [10] => 2006-09-30 [11] => 2006-10-17 [12] => 2006-10-27 [13] => 2006-11-14 [14] => 2006-11-14 [15] => 2006-11-14 [16] => 2006-11-20 [17] => 2006-11-20 )


I thought i could use explode at "-" and then a foreach but i thought that might take a long time if i did have a lot of dates in the array.

is there an easy way to do this? just so i get the output something like array (5, 6, 7, 8, 9, 10, 11) denoting months.
many thanks
Link to comment
https://forums.phpfreaks.com/topic/27960-find-next-month-in-sequence-of-dates/
Share on other sites

Just another way...

[code]<?php

$data = array ( 0 => '2006-05-18', 1 => '2006-05-21', 2 => '2006-06-23', 3 => '2006-06-27', 4 => '2006-07-17', 5 => '2006-07-17', 6 => '2006-08-01', 7 => '2006-08-01', 8 => '2006-09-14', 9 => '2006-09-17', 10 => '2006-09-30', 11 => '2006-10-17', 12 => '2006-10-27', 13 => '2006-11-14', 14 => '2006-11-14', 15 => '2006-11-14', 16 => '2006-11-20', 17 => '2006-11-20' );

$data = array_unique ( array_map ( create_function ( '$date', 'return intval ( substr ( $date, 5, 2 ) );' ), $data ) );

print_r ( $data );

?>[/code]


printf
thanks very much for the replies.

in the end i did this as i realised i need to have all the months inbetween the first and the last one regardless of them actually being in the initial array.

                for ($fm=$firstMonth; $fm < $lastMonth+1; $fm++)
{
$monthArray[$fm] = $fm;
$days_in_month[$fm] = cal_days_in_month(CAL_GREGORIAN, $monthArray[$fm], $currYear);
}

Thanks again. Just one quick thing though whats the code for getting those nice formatted text boxes for dropping code in?

Cheers

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.