-2

So this is my code for set the date from selected date to auto set date excluding weekend (sat&sun) and holiday ( i put it in database ) :

function number_of_working_dates($from, $days) {

  /* get holiday from database */
  include('includes/config.php');
  $sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
  $qqq = mysqli_query($konek,$sss);
  $arr = array();
  while ( $tam = mysqli_fetch_array($qqq)) {
    $date = $tam['tanggal_awal'];
    $reformat_date =  date("Y-m-d", strtotime($date));
    $arr[] = $reformat_date; 
  }
  $array_date_2 = '"'.implode('", "' , $arr) . '"';

The output of implode() is a list of dates:

"2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"

Then the continuation:

    $holidayDays = [$array_date_2];
    //$holidayDays = ["2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"]
    $from = new DateTime($from);
    $dates = [];
    $dates[] = $from->format('Y-m-d');
    **echo count($holidayDays);**

    while ($days) {
        $from->modify('+1 day');

        if (!in_array($from->format('N'), $workingDays)) continue;
        if (in_array($from->format('Y-m-d'), $holidayDays)) continue;
        if (in_array($from->format('*-m-d'), $holidayDays)) continue;

        $dates[] = $from->format('Y-m-d');
        $days--;
    }
    return $dates;
}

When I count the "holidayDays" array using "count" it only shows 1 item, but when I hardcoded the date list like in the comment line it works and shows 4 items.

Can someone help me?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Kiki Nice
  • 29
  • 1
  • 7
  • Im not sure, but could it be you count a string instead of an array? – Ronnie Oosting Aug 08 '18 at 11:39
  • 2
    Your question doesn't make sense. `$holidayDays = [$array_date_2];` is clearly an array with one thing in it, why would it be anything else? `$holidayDays = "2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"` throws an exception: Parse error: syntax error, unexpected ',' – Quentin Aug 08 '18 at 11:39
  • Do you mean: `$holidayDays[] = $array_date_2;` ? – Martin Aug 08 '18 at 11:40
  • You need to define what is inside $array_date_2. try `var_dump`, and check what results you get in return. – Ronnie Oosting Aug 08 '18 at 11:40
  • `$holidayDays = [$array_date_2];` will be an array containing a single item. That item is a string listing all your dates. So yes, the count is 1, because the array contains 1 string. the "implode" function produces a string. It's not clear what else you were expecting. http://php.net/manual/en/function.implode.php . P.S. Your hard-coded `//$holidayDays = ["2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"]` is an array containing four _separate_ strings, so its count is, logically, 4. Do `var_dump($holidayDays)`; and you can see the variable's structure. – ADyson Aug 08 '18 at 11:42
  • `[$foo]` is an array literal for an array with exactly one value, as written. `["foo", "bar", "baz"]` is an array literal for an array with three values, exactly as written. Using a variable in that array literal doesn't work like copy and pasting code. It does not matter what the content of `$foo` is, `[$foo]` always means the same thing: an array with one element. – You already have an array, why do you need to `implode` and re-interpret it as an array? – deceze Aug 08 '18 at 11:43
  • thank for the comment, im a newbie, i just want to put the list of date that i get from database to be like when i hardcoded it. but when i implode it, it does not work as i aspected. – Kiki Nice Aug 08 '18 at 11:44
  • as mentioned, it's because implode() produces a **single string**. not clear why you even need to implode it. You seem to want $holidayDays to be an array. $arr was an array to begin with before you imploded it. So `$holidayDays = $arr;` might be all you need. – ADyson Aug 08 '18 at 11:45
  • i use $holidayDays = $arr; but it still count 0, not 4 as i aspected. i need the list of date to be like ["2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"] this format. – Kiki Nice Aug 08 '18 at 11:50
  • if count($arr) is 0 it must be because your query returned no rows. – ADyson Aug 08 '18 at 12:05

2 Answers2

2

A variable containing a string contains that string and it will continue to be treated as a string unless you do something like evaluating it as source code (which is very dangerous).

So if you have:

$foo = ["1", "2", "3"];

Then you have an array with three things in it.

But if you have:

$string = '"1", "2", "3"';
$foo = [$string];

Then you have an array with one thing in it: a string.

It is the same as writing:

$foo = ['"1", "2", "3"'];

If you want to have an array of strings then look at this line of your code:

$array_date_2 = '"'.implode('", "' , $arr) . '"';

That converts the array of strings into a single string.

Do not do that because that is not what you want.

Just use the value you already have in $arr.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • yes thank you i got your point, the list of array i get from DB change into a single string so it why when i counted it, it show 1, so what should i do to put that list of date i get from database with the format like the hardcoded date? – Kiki Nice Aug 08 '18 at 11:54
  • @iqbalnahdi — I refer you back to the last two sentences of this answer. – Quentin Aug 08 '18 at 11:55
  • thank quentin, i use your solution and the date output is : "2018-08-09", "2018-08-10","2018-08-15", "2018-08-17" which is i want it, but it still count as 1 array, how to make it 4 ?, sorry im a newbie – Kiki Nice Aug 08 '18 at 12:07
  • 2
    @iqbalnahdi quentin's solution is effectively the same as mine in the comments above - i.e. "you can just use $arr". When you replied to me you claimed this returned 0 results, now you claim it returns ""2018-08-09", "2018-08-10","2018-08-15", "2018-08-17", yet you also claim this counts as one item in the array. None of this makes any sense. What did you actually do? You must have done two different things. Similar to what I said above, if you wrote `$holidayDays = $arr;` then the only reason it could contain 0 items is if your query returned no rows. Not sure how you got the other output. – ADyson Aug 08 '18 at 12:18
-1

You have a variable named "$array_date_2" and are trying to assign a String to it with the following code :

  $array_date_2 = '"'.implode('", "' , $arr) . '"';

This assignment , after the "implode" has been executed , would result in the following :

  $array_date_2 = ""2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"";

Notice the quotes at the start and end of the String. Even though you do not see them when "echoing" the String is wrapped around those quotes. PHP does not permit a String without it being enclosed in quotes , it will throw a Parse Error. So in the end you do not have a String like :

"2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"

But more like

""2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17""

Now you are also trying to create an array by concatenating Strings and Special Characters together. This is not how PHP works though.

If you want to "create" a function out of a String , or generally evaluate a String as PHP Code , then you need to use "eval" but i would strongly suggest against it.

For reference only , if you went with eval your code should be like this :

eval("\$holidayDays = [".$array_date_2 ."];");
Strahdvonzar
  • 400
  • 2
  • 10
  • thank for the comment, no it not, the output of $array_date_2 is "2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17", and when i harcoded it to the $holidayDays = ["2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"] it work well, put when i put $holidayDays = [ $array_date_2 ] it does not work, and show only 1 array. – Kiki Nice Aug 08 '18 at 12:20
  • @iqbalnahdi "no it not"...sorry but yes it is...read the answer: "Even though you do not see them when "echoing" the String is wrapped around those quotes." Demo using var_dump: http://sandbox.onlinephpfunctions.com/code/d97501aba5b6a1dc5548eab496d945279b3892e5 . I've also included dumps of all the other combinations of variables we've been talking about, just to demonstrate what we mean. P.S. In general, var_dump() is more useful than echo for debugging purposes. – ADyson Aug 08 '18 at 13:25
  • I was apparently not comprehensive enough in my answer , even though i specifically included a part covering your wrongful assumption that there are no double quotes around your String. As ADyson stated , use var_dump and you will understand what i am talking about. Don't rush to downvote an answer. Take your time , read it , try it out , and provide useful feedback if there is an error in it. – Strahdvonzar Aug 08 '18 at 14:32