0

Right now my program echo an array of dates generated with for loop. What I'm trying to do is echo dates individually.

Initial code

 // initialize an array with your first date
    $dates = array(strtotime("+11 days", strtotime("2017-09-04")));

    for ($i = 1; $i <= 5; $i++) {// loop 5 times to get the next 5 dates

        // add 7 days to previous date in the array
        $dates[] = strtotime("+7 days", $dates[$i-1]);
    }

    // echo the results
    foreach ($dates as $date) {
        echo date("Y-m-d", $date);
        echo "<br>";
    }

Initial Output

2017-09-15
2017-09-22
2017-09-29
2017-10-06
2017-10-13

What I have tried

echo $dates[0];//print first date
echo "<br>";
echo $dates[1];//print second date

Trial Output

1505426400
1506031200

How can I achieve this?

5
  • either change the code that puts the date in the array to include the date("Y-m-d", $date); or do that on output Commented Sep 5, 2017 at 0:03
  • 1
    Either overwrite/alter the values in the array when you convert it to the date-format (you can do that by passing by reference, foreach ($dates as &$date), note the &), or convert from timestamp to a datestring when you output the values, echo date("Y-m-d", $dates[0]); Commented Sep 5, 2017 at 0:07
  • I'm voting to close this question as off-topic because the OP knew which function to use to format the timestamp and simply didn't do it. This makes the question (and page) low-to-no value for future readers. Commented Sep 5, 2017 at 2:26
  • @mickmackusa: Not at all true. If I knew I would have not posted in the first place. I'm not here to waste nobody's time. I'm on tight schedule too. The reason why I agreed with rtfm it's because I realized how simple it was. I think it is valuable for any noobie. Commented Sep 5, 2017 at 2:56
  • Please understand that I am not calling you a bad person/poster. I merely believe this question is akin to a typo question and ultimately won't be valuable or searchable (people with a similar problem are not likely to find this page). You knew what to do, it just didn't make it into the code. I am happy that SO volunteers have helped you to remedy your issue. Commented Sep 5, 2017 at 5:46

2 Answers 2

2

Either you need to use date() when outputting the elements as well - as they still are timestamps in the array (you don't change anything in the loop, you just print the elements), or you need to change the elements when you loop over them.

Alternative 1: Format on output

Convert from timestamp to datestring on output. This will still have timestamps in the array.

echo date("Y-m-d", $dates[0]);

Alternative 2: Alter the elements in the array

Alternatively, you can alter the value in the array when you loop it through foreach. If you pass by reference, you can change the value of the element inside the loop, by using & (which means that the variable is a reference and not a copy). This means that you now have datestrings in the array, and not timestamps.

foreach ($dates as &$date) {
    echo $date = date("Y-m-d", $date);
    echo "<br>";
}

If you pass by reference, you can now print it directly, as it will no longer contain the timestamp, since we changed it to the datestring.

echo $dates[0];

You should only adapt one of the alternatives, whichever is most suitable for your application.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

echo date("Y-m-d", ($dates[0])) . '<br>';
echo date("Y-m-d", ($dates[1])) . '<br>';

1 Comment

While answers are always appreciated, it really helps to provide some additional info regarding how your code solves the problem at hand. Doing so aids those with similar (but not identical) problems. Not everyone may be familiar with your exact coding logic, but may understand your general approach or concept. To help improve your answer, please provide some context surrounding it, and see the help article on writing great answers for some tips on how to make your answers count :)

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.