AJLX Posted February 19, 2014 Share Posted February 19, 2014 Hello guys. I have a DB that has a Project Id, Start_date, End_date in it. I want to be able to put an array that gives me the date range, and then have the project id associtated with it. I currently get the date range, by using this function: function getDateRange($startDate, $endDate, $format="Y-m-d") { //Create output variable $datesArray = array(); //Calculate number of days in the range $total_days = round(abs(strtotime($endDate) - strtotime($startDate)) / 86400, 0) + 1; if($days<0) { return false; } //Populate array of weekdays and counts for($day=0; $day<$total_days; $day++) { $datesArray[] = date($format, strtotime("{$startDate} + {$day} days")); } //Return results array return $datesArray; } $dateRange = array(); $result = mysql_query("SELECT * FROM Project_details WHERE Project_id ='$value'"); while ($row = mysql_fetch_array($result)) { $project_id = ($row['Project_id']); $startDate = date("d-M-y", strtotime($row['Start_date'])); // get start date for current project and format it to look correct $endDate = date("d-M-y", strtotime($row['End_date'])); // get start date for current project and format it to look correct $dateRange[] = getDateRange($startDate,$endDate); } The start This gives me a the date range. I would like to now add the project id to each entry so that the array would look something like this: date => 17.02.14 project_id => 15 date => 18.02.14 project_id => 15 date => 19.02.14 project_id => 15 date => 20.02.14 project_id => 15 date => 21.02.14 project_id => 15 I have had a tweak, but the project number always seems to increment instead. Quote Link to comment https://forums.phpfreaks.com/topic/286320-php-array-issues/ Share on other sites More sharing options...
ginerjm Posted February 19, 2014 Share Posted February 19, 2014 (edited) Use your date value as the key value of your array and assign the project id as the value of each item in the array $dates_array['17-02-14'] = 15 $dates_array['18-02-14'] = 15 Pass the id to the function and change the function from: $datesArray[] = date($format, strtotime("{$startDate} + {$day} days")); to: $k = date($format, strtotime("{$startDate} + {$day} days")); $dates_array[$k] = $proj_id; where $proj_id is passed in as a param Edited February 19, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/286320-php-array-issues/#findComment-1469567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.