Hi all:

I have an array of dates being posted to a page as Y-m-d. I am adding a space after each comma in the array. The code to display this is simply as follows:

str_replace(",",", ",$pShowDateArray_str)

The problem is I want them displayed in n/j/Y format. I have tried the following and all are causing the dates not to appear:

1. str_replace(",",", ",date_format(date_create($pShowDateArray_str), "n/j/Y")


2. $vShowDateArrayNJY =  date_format(date_create($pShowDateArray_str,"n/j/Y");
str_replace(",",", ",$vShowDateArrayNJY)

I have tried a few other ways too but after about 2 hours on this I can't remember 😕

Any help appreciated

    Why does an "array of dates" have a comma anywhere in it to begin with? Note that this:

    "2011-06-01,2011-06-02,2011-06-03"

    is not an array of dates.. it's just a string with some numbers and commas and whatnot in it.

    Regardless, you're going to have to process the dates one-by-one (which would be quite easy if you actually had an array).

      Yep (as usual) you are correct. It is a string and not an array of dates.

      But what am I doing wrong? here is the code on the form page that it is posting from:

      
      if (isset($rDate1)){
      					echo "<input type='checkbox' name='showDate[]' value='".date_format(date_create($rDate1),"Y-m-d")."' /> ".date_format(date_create($rDate1),"n/j/Y")."<br />";
      				}
      				if (isset($rDate2)){
      					echo "<input type='checkbox' name='showDate[]' value='".date_format(date_create($rDate2),"Y-m-d")."' /> ".date_format(date_create($rDate2),"n/j/Y")."<br />";
      				}
      				if (isset($rDate3)){
      					echo "<input type='checkbox' name='showDate[]' value='".date_format(date_create($rDate3),"Y-m-d")."' /> ".date_format(date_create($rDate3),"n/j/Y")."<br />";
      				}
      				if (isset($rDate4)){
      					echo "<input type='checkbox' name='showDate[]' value='".date_format(date_create($rDate4),"Y-m-d")."' /> ".date_format(date_create($rDate4),"n/j/Y")."<br />";
      				}
      				if (isset($rDateLast)){
      					echo "<input type='checkbox' name='showDate[]' value='".date_format(date_create($rDateLast),"Y-m-d")."' /> ".date_format(date_create($rDateLast),"n/j/Y")."<br />";
      

        Out of curiosity... where are all of those $rDate variables coming from? Seems silly to number/name them in such a way - why not just create a single variable $rDate as an array of dates; then you could reduce all of that down to a two-line [man]foreach/man loop.

        Regardless, what you've shown above should result in an array being created, namely $POST['showDate']. Not sure what you're trying to do from there... if you want to change the format of the dates, why POST them in Y-m-d format in the first place? If you use them in that format but also want to re-display them again in a different format, simply use a foreach loop to loop over the $POST['showDate'] array and build a new array with the same values in whatever format you like.

          Thank you. I will try your suggestion.

            Write a Reply...