如何回应日期数组中的各个日期?

时间:2021-10-16 15:43:15

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

现在我的程序回显了一个用for循环生成的日期数组。我要做的是单独回复日期。

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?

我怎样才能做到这一点?

2 个解决方案

#1


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.

你也需要在输出元素时使用date() - 因为它们仍然是数组中的时间戳(你不会在循环中改变任何东西,你只需要打印元素),或者你需要更改元素循环过来。

Alternative 1: Format on output

备选方案1:输出格式

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

在输出上从timestamp转换为datestring。这仍然会在数组中有时间戳。

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

Alternative 2: Alter the elements in the array

备选方案2:更改阵列中的元素

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时更改数组中的值。如果通过引用传递,则可以使用&更改循环内元素的值(这意味着变量是引用而不是副本)。这意味着您现在在数组中有datetring,而不是时间戳。

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.

如果您通过引用传递,您现在可以直接打印它,因为它将不再包含时间戳,因为我们将其更改为datestring。

echo $dates[0];

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

您应该只选择其中一种替代方案,以最适合您的应用为准。

#2


0  

try this

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

#1


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.

你也需要在输出元素时使用date() - 因为它们仍然是数组中的时间戳(你不会在循环中改变任何东西,你只需要打印元素),或者你需要更改元素循环过来。

Alternative 1: Format on output

备选方案1:输出格式

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

在输出上从timestamp转换为datestring。这仍然会在数组中有时间戳。

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

Alternative 2: Alter the elements in the array

备选方案2:更改阵列中的元素

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时更改数组中的值。如果通过引用传递,则可以使用&更改循环内元素的值(这意味着变量是引用而不是副本)。这意味着您现在在数组中有datetring,而不是时间戳。

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.

如果您通过引用传递,您现在可以直接打印它,因为它将不再包含时间戳,因为我们将其更改为datestring。

echo $dates[0];

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

您应该只选择其中一种替代方案,以最适合您的应用为准。

#2


0  

try this

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