Jump to content

PHP Array Issues


AJLX

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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 by ginerjm
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.