如何从给定的开始日期和结束日期起每隔两周获取星期一和星期三的日期
例如
Start date : 01-01-2019
End Date : 05-03-2019
每两周的星期一和星期三
因此以下日期应在列表中
Wednesday : 02-01-2019
Monday : 14-01-2019
Wednesday : 16-01-2019
Monday : 28-01-2019
Wednesday : 30-01-2019
Monday : 11-02-2019
Wednesday : 13-02-2019
Monday : 25-02-2019
Wednesday : 27-02-2019
$startDate = '01-01-2019';
$endDate = '01-03-2019';
$day_number = '1';
$ofWeek = '2';
getDateForSpecificDayBetweenDates($startDate,$endDate,$day_number,$ofWeek);
function getDateForSpecificDayBetweenDates($startDate,$endDate,$day_number,$ofWeek){
$endDate = strtotime($endDate);
$days=array('1'=>'Monday','2' => 'Tuesday','3' => 'Wednesday','4'=>'Thursday','5' =>'Friday','6' => 'Saturday','7'=>'Sunday');
$date_array = array();
$cnt=0;
for($i = strtotime($days[$day_number], strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
{
$weeknumber = weekOfMonth(date('Y-m-d',$i));
echo "Week : ".$weeknumber.' - Date : '.date('Y-m-d',$i)."<br>";
if($weeknumber == $ofWeek){
$date_array[$cnt]['dates']=date('Y-m-d',$i);
$date_array[$cnt]['week']=$weeknumber;
$cnt++;
}
}
echo "<pre>";print_r($date_array);echo "<pre>";
}
function weekOfMonth($date) {
// estract date parts
list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));
// current week, min 1
$w = 1;
// for each day since the start of the month
for ($i = 1; $i <= $d; ++$i) {
// if that day was a sunday and is not the first day of month
if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0) {
// increment current week
++$w;
}
}
// now return
return $w;
}
我曾经使用过开发此代码,但它只给我每月2周的时间,开始日期后每2周需要
我无法从范围中找到第二周。如何确定第二周的日期
任何帮助都将被
答案 0 :(得分:1)
您可以使用DatePeriod
来实现:
$begin = new DateTime('2019-01-01');
$end = new DateTime('2019-03-05');
$interval = new DateInterval('P2W');
$period = new DatePeriod($begin, $interval, $end);
$dates = [];
foreach ($period as $date) {
$wednesday = $date->modify('next wednesday');
$monday = (clone $wednesday)->modify('+1 week next monday');
if ($wednesday < $end) {
$dates[] = $wednesday;
}
if ($monday < $end) {
$dates[] = $monday;
}
}
foreach ($dates as $date) {
echo $date->format('D: d-m-Y') . '<br />';
}
结果是:
Wed: 02-01-2019
Mon: 14-01-2019
Wed: 16-01-2019
Mon: 28-01-2019
Wed: 30-01-2019
Mon: 11-02-2019
Wed: 13-02-2019
Mon: 25-02-2019
Wed: 27-02-2019
检查here
答案 1 :(得分:1)
<?php
$date1 = new DateTime('01-01-2019');
$date2 = new DateTime('05-03-2019');
$interval = $date1->diff($date2);
$weekday = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
while($date1<$date2)
{
$day_of_week = intval($date1->format('w'));
if($day_of_week>=1 && $day_of_week<3)
{
$addDays=3-$day_of_week;
}else{
$addDays=15-$day_of_week;
}
$date1->modify('+' . $addDays . ' day');
$day_of_week = intval($date1->format('w')) - 1;
if($date1<$date2)
{
echo $weekday[$day_of_week] . ' - ' . $date1->format('d-m-Y') . '<br>';
}
}
?>
您可以在php小提琴上尝试一下。
首先我放假一天。
然后检查是否是星期一或星期二,是否与3相差,因为星期三是一周的第三天,并将其添加到当天。
那么您有星期一,应该是星期三之后的第二个星期一。
两个星期一之间的差值为15天,因此从15减去。
这里是输出。
wednesday - 02-01-2019
monday - 14-01-2019
wednesday - 16-01-2019
monday - 28-01-2019
wednesday - 30-01-2019
monday - 11-02-2019
wednesday - 13-02-2019
monday - 25-02-2019
wednesday - 27-02-2019
答案 2 :(得分:1)
这看起来像是一个有趣的挑战。
这是使用DateTime对象和->modify()
函数移动到下一个星期一和星期三的一种方法。
<?php
$start_date = '01-01-2019';
$end_date = '05-03-2019';
$sd = new DateTimeImmutable($start_date);
$nd = new DateTime($start_date);
$ed = new DateTimeImmutable($end_date);
echo 'Start Date = ' . $sd->format('D Y-m-d').PHP_EOL;
// check if the next date from the start date is a monday or a wednesday
// and output the first date accordingly
if ( $sd->modify('next monday') < $sd->modify('next wednesday')) {
echo '>>>' . $nd->modify('next monday')->format('D d/m/Y'). PHP_EOL;
echo '>>>' . $nd->modify('next wednesday')->format('D d/m/Y'). PHP_EOL;
}else{
echo '>>>' . $nd->modify('next wednesday')->format('D d/m/Y'). PHP_EOL;
}
while (1) {
// add 7 days
$nd->add(new DateInterval('P7D'));
// go to next monday unless that means we went past the end date
if ( $nd->modify('next monday') > $ed ) { break; }
echo '>>>' . $nd->format('D d/m/Y'). PHP_EOL;
// go to next wednesday unless that means we went past the end date
if ( $nd->modify('next wednesday') > $ed ) { break; }
echo '>>>' . $nd->format('D d/m/Y'). PHP_EOL;
}
结果
Start Date = Tue 2019-01-01
>>>Wed 02/01/2019
>>>Mon 14/01/2019
>>>Wed 16/01/2019
>>>Mon 28/01/2019
>>>Wed 30/01/2019
>>>Mon 11/02/2019
>>>Wed 13/02/2019
>>>Mon 25/02/2019
>>>Wed 27/02/2019
答案 3 :(得分:0)
Try此函数传递两个日期时间戳:
<?php
function get_date($startDateTime,$repeat_until){
for ($i = $startDateTime; $i <= $repeat_until; $i+=86400) {
$ShowDay_arr = array(1, 3);
$Newdate = $i;
$Numerday=date("N",$Newdate);
if(in_array($Numerday,$ShowDay_arr)){
$day = date("D", $Newdate);
echo "<b> Day : ".$day."</b> (".date("d-m-Y",$Newdate).")";
echo "<br>";
}
}
}
get_date(1559401144,1561906744);
?>