PHP date range, array

I have a date range selected from a database. For this specific example, the date is between June 20 - June 29.

I managed to create an array of these dates. However, the date is repeated many 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. But is it only 9 days?

I have a feeling that 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("Ymd", $i); } } } 

How to get only 9 instances in my array, not 89?

0
date php
source share
5 answers

What is the purpose of the next line?

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

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

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

Try array_unique

 $times = array_unique($times); 
+4
source share

Use the array_unique function.

An example 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 ) 
+1
source share

Because your array generates multiple instances of the "day" date for each user (you have 9 users!), You really don't need to pull your user list to create a date range

You can simply use the code below without involving the user to create your date range and then make your userpull separately, passing this array for comparison / validation.

 $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("Ymd", $i); } } 

As suggested in other answers, you can use array_unique () to fix this, but tbh I would look at your entire logical stream, there is no need for loops in loops to generate start and end dates. Even less soo if you pull the min / max range from available users in the mysql database (find MIN () MAX () and DISTINCT)

+1
source share

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("Ymd", $i),$times)){ $times []= date("Ymd", $i); } } } } 

This way you check that the data already exists in this array, and if it does not exist, it will be added to the array

+1
source share

All Articles