0

I have a date range selected from the Database. For this particular example the date is between the 20th June - 29th June.

I have managed to create an array of those dates, However the date is repeated numerous times.

My array looks like this :

Array
(
 [0] => 2013-06-20
 [1] => 2013-06-20
 [2] => 2013-06-20
 [3] => 2013-06-20
 [4] => 2013-06-20
 [5] => 2013-06-20
 [6] => 2013-06-20
 [7] => 2013-06-20
 [8] => 2013-06-20
 [9] => 2013-06-21
 [10] => 2013-06-21
 [11] => 2013-06-21
 [12] => 2013-06-21
 [13] => 2013-06-21
 [14] => 2013-06-21
 [15] => 2013-06-21
 [16] => 2013-06-21
 [17] => 2013-06-21
 [18] => 2013-06-22
 [19] => 2013-06-22
 [20] => 2013-06-22
 [21] => 2013-06-22
 [22] => 2013-06-22
 [23] => 2013-06-22
 [24] => 2013-06-22
 [25] => 2013-06-22
 [26] => 2013-06-22
 [27] => 2013-06-23
 [28] => 2013-06-23
 [29] => 2013-06-23
 [30] => 2013-06-23
 [31] => 2013-06-23
 [32] => 2013-06-23
 [33] => 2013-06-23
 [34] => 2013-06-23
 [35] => 2013-06-23
 [36] => 2013-06-24
 [37] => 2013-06-24
 [38] => 2013-06-24
 [39] => 2013-06-24
 [40] => 2013-06-24
 [41] => 2013-06-24
 [42] => 2013-06-24
 [43] => 2013-06-24
 [44] => 2013-06-24
 [45] => 2013-06-25
 [46] => 2013-06-25
 [47] => 2013-06-25
 [48] => 2013-06-25
 [49] => 2013-06-25
 [50] => 2013-06-25
 [51] => 2013-06-25
 [52] => 2013-06-25
 [53] => 2013-06-25
 [54] => 2013-06-26
 [55] => 2013-06-26
 [56] => 2013-06-26
 [57] => 2013-06-26
 [58] => 2013-06-26
 [59] => 2013-06-26
 [60] => 2013-06-26
 [61] => 2013-06-26
 [62] => 2013-06-26
 [63] => 2013-06-27
 [64] => 2013-06-27
 [65] => 2013-06-27
 [66] => 2013-06-27
 [67] => 2013-06-27
 [68] => 2013-06-27
 [69] => 2013-06-27
 [70] => 2013-06-27
 [71] => 2013-06-27
 [72] => 2013-06-28
 [73] => 2013-06-28
 [74] => 2013-06-28
 [75] => 2013-06-28
 [76] => 2013-06-28
 [77] => 2013-06-28
 [78] => 2013-06-28
 [79] => 2013-06-28
 [80] => 2013-06-28
 [81] => 2013-06-29
 [82] => 2013-06-29
 [83] => 2013-06-29
 [84] => 2013-06-29
 [85] => 2013-06-29
 [86] => 2013-06-29
 [87] => 2013-06-29
 [88] => 2013-06-29
 [89] => 2013-06-29
)

As you can see from the array, There are 89 instances. Yet this is for only 9 days?

I have a feeling it has something to do with my for loop.

My PHP code is as follows :

$user_availabilty = $this->vendor_model->get_user_availability($this->get_user_id());

        if($user_availabilty)
        {
            foreach($user_availabilty as $user_avail)
            {
                $start = strtotime($user_avail->user_availablity_startdate);
                $end = strtotime($user_avail->user_availablity_enddate);

                $times = array();

                for ($i = $start; $i <= $end; $i += 24 * 3600)
                {
                    for ($j = 9; $j <= 17; $j++)
                    {
                        $times []= date("Y-m-d", $i);
                    }
                }
            }

How do I get just the 9 instances into my array, rather than 89?

1
  • How are you retrieving the data? Are you usng a query on a MySQL database? If so, can you post your query and a table structure? Commented Jun 19, 2013 at 8:54

5 Answers 5

5

Try with array_unique

$times = array_unique($times);
Sign up to request clarification or add additional context in comments.

Comments

2

What's the purpose of following line?

for ($j = 9; $j <= 17; $j++)

I think you need just remark this line to get what you want.

// for ($j = 9; $j <= 17; $j++)

Comments

1

Use the array_unique function.

Example straight from the PHP Manual.

<?php
  $input = array("a" => "green", "red", "b" => "green", "blue", "red");
  $result = array_unique($input);
  print_r($result);
?>

The output will be

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

Comments

1

Its because your array is generating multiple instances of the "day" date for every user (you have 9 users!) you really don't need to pull your userlist to generate the date range

You can just use the code below without the user pull to generate your date range and then do your userpull separately passing this array in for comparison/checking.

$start = strtotime($user_avail->user_availablity_startdate);
        $end = strtotime($user_avail->user_availablity_enddate);

        $times = array();

        for ($i = $start; $i <= $end; $i += 24 * 3600)
        {
            for ($j = 9; $j <= 17; $j++)
            {
                $times []= date("Y-m-d", $i);
            }
        }

As suggested in other answers you can use array_unique() to fix it up but tbh I'd look at your entire logic flow there's no need for loops within loops to generate a start and end date. Even less soo if you're pulling a min/max range from available users within a mysql database (look up MIN() MAX() and DISTINCT)

Comments

1

Use this

$user_availabilty = $this->vendor_model->get_user_availability($this->get_user_id());

        if($user_availabilty)
        {
            foreach($user_availabilty as $user_avail)
            {
                $start = strtotime($user_avail->user_availablity_startdate);
                $end = strtotime($user_avail->user_availablity_enddate);

                $times = array();

                for ($i = $start; $i <= $end; $i += 24 * 3600)
                {
                    for ($j = 9; $j <= 17; $j++)
                    {
                          if (!in_array(date("Y-m-d", $i),$times)){

                            $times []= date("Y-m-d", $i);
                              }

                    }
                }
            }

By this you are checking the data already exists in that array and if it does not exists, it will be added into the array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.