c_shelswell Posted November 21, 2006 Share Posted November 21, 2006 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 More sharing options...
Orio Posted November 21, 2006 Share Posted November 21, 2006 I cant think of a better way than that...[code]<?php$dates = array("2006-05-18", ....); //enter the months$months = array();foreach ($dates as $date){ $temp = explode($date); $months[]=$temp[1];}?>[/code]Then you can continue however you want...Orio. Link to comment https://forums.phpfreaks.com/topic/27960-find-next-month-in-sequence-of-dates/#findComment-127904 Share on other sites More sharing options...
printf Posted November 21, 2006 Share Posted November 21, 2006 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 Link to comment https://forums.phpfreaks.com/topic/27960-find-next-month-in-sequence-of-dates/#findComment-127927 Share on other sites More sharing options...
c_shelswell Posted November 21, 2006 Author Share Posted November 21, 2006 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 Link to comment https://forums.phpfreaks.com/topic/27960-find-next-month-in-sequence-of-dates/#findComment-127933 Share on other sites More sharing options...
kenrbnsn Posted November 21, 2006 Share Posted November 21, 2006 [quote]Just one quick thing though whats the code for getting those nice formatted text boxes for dropping code in?[/quote]Surround your code with [b][nobbc][code][/code][/nobbc][/b] tags.Ken Link to comment https://forums.phpfreaks.com/topic/27960-find-next-month-in-sequence-of-dates/#findComment-127960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.