[英]I would like to put every other wednesday into an array using php
My Issue:我的问题:
I have found a way to select every other Wednesday here: PHP Select every other Wednesday with the following code.我在这里找到了一种每隔一个星期三选择的方法: PHP 每隔一个星期三选择一次,代码如下。
<?php
$number_of_dates = 10;
$start_date = new DateTime("1/1/20");
$interval = DateInterval::createFromDateString("second wednesday");
$period = new DatePeriod($start_date, $interval, $number_of_dates - 1);
foreach ($period as $date) {
$datex = $date->format("m-d-Y").PHP_EOL;
echo "$datex<br>";
}
?>
What I need to do is put every other Wednesday into an array.我需要做的是将每隔一个星期三放入一个数组中。
I can put a range of dates into an array, but it uses every day in the range with the following code.我可以将一系列日期放入一个数组中,但它使用以下代码在该范围内的每一天。 I just need it to be every other Wednesday.
我只需要每隔一个星期三。 How can I do this?
我怎样才能做到这一点?
<?PHP
$dates = array();
$datetime1 = new DateTime("2020-01-01");
$datetime2 = new DateTime("2020-1-31");
$interval = $datetime1->diff($datetime2);
$days = (int) $interval->format('%R%a');
$currentTimestamp = $datetime1->getTimestamp();
$dates[] = date("m/d/Y", $currentTimestamp);
for ($x = 0; $x < $days; $x++)
{
$currentTimestamp = strtotime("+1 day", $currentTimestamp);
$dates[] = date("m/d/Y", $currentTimestamp);
}
print_r($dates);
?>
Try this :尝试这个 :
<?php
$dates = array();
$datetime = new DateTime();
for ($i = 0; $i < 52; $i++) {
$datetime->modify('next Wednesday');
array_push($dates, $datetime->format('m/d/Y'));
}
print_r($dates);
What does it do ?它有什么作用 ?
Live Demo : http://sandbox.onlinephpfunctions.com/code/57c6a6b682a07f75bc1c507c588c844d84330610现场演示: http : //sandbox.onlinephpfunctions.com/code/57c6a6b682a07f75bc1c507c588c844d84330610
Regards问候
Your first snippet is already outputting the right set of dates, so you just would need to put each of those in an array, instead of echoing them.您的第一个片段已经输出了正确的日期集,因此您只需要将每个日期放在一个数组中,而不是回显它们。
(Although I don't know if it's really necessary to do so, since you can already loop over the $period
variable - but it depends exactly what you plan to do with the data afterwards). (虽然我不知道是否真的有必要这样做,因为您已经可以遍历
$period
变量 - 但这完全取决于您之后打算对数据做什么)。
Example:例子:
$number_of_dates = 10;
$start_date = new DateTime("1/1/20");
$interval = DateInterval::createFromDateString("second wednesday");
$period = new DatePeriod($start_date, $interval, $number_of_dates - 1);
$dates = array(); //declare a new empty array
foreach ($period as $date) {
$dates[] = $date; //add the date to the next empty array index
}
var_dump($dates);
Or if you actually want an array containing the string representations of those dates, in that specific format, then:或者,如果您确实想要一个包含这些日期的字符串表示的数组,以该特定格式,则:
$dates2 = array();
foreach ($period as $date) {
$dates2[] = $date->format("m-d-Y");
}
var_dump($dates2);
Live demo: http://sandbox.onlinephpfunctions.com/code/9e58e552edff204ae6df3ca9b437b8c597edf2b3现场演示: http : //sandbox.onlinephpfunctions.com/code/9e58e552edff204ae6df3ca9b437b8c597edf2b3
Give it a try试一试
<?PHP
$dates = [];
$datetime1 = new DateTime("2020-01-01");
$datetime2 = new DateTime("2020-01-31");
$datetime1->modify('wednesday'); // Start with wednesday
while($datetime1 < $datetime2){
$dates[] = $datetime1->format('Y-m-d');
$datetime1->modify('second wednesday'); // Other Wednesday
}
print_r($dates);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.