我的字符串用逗号分隔,包含日期列表。这个列表是用mySQL" Group_concat"命令看起来像这样:
11.01.2017, 18.01.2017, 25.01.2017, 01.02.2017, 08.02.2017, 01.03.2017, 08.03.2017
日期与工作日完全相同,例如星期一。在2017年2月15日和20日的例子中,列表中缺少一些星期一。
我正在寻找列表中缺少的星期一,并且到目前为止还有这段代码:
$weekday = 1
//$Kurstage = explode(", ",$rowK->listagg_2 );
$Kurstage = explode(", ","11.01.2017, 18.01.2017, 25.01.2017, 01.02.2017, 08.02.2017, 01.03.2017, 08.03.2017" );
$dateStart = date_create( "11.01.2017");
$dateEnd = date_create( "08.03.2017");
$missing_dates = array();
$interval = new DateInterval('P1D');
$period = new DatePeriod($dateStart, $interval, $dateEnd);
echo "dateStart:". $dateStart->format( "Y-m-d" ) . ", dateEnd: " .$dateEnd->format( "Y-m-d" )."<br />";
foreach($period as $day)
{
if ( $day->format( "w" ) == $weekday )
{
if (! in_array( $day, $Kurstage))
{
echo $rowK->code. " - ". $day->format( "Y-m-d" ) ."<br />";
}
}
}
我认为问题是,使用explode创建的数组中的日期不会被识别为日期。
答案 0 :(得分:0)
看起来你有周三而不是星期一的列表所以试试这个:
$weekday = 3;
和你的第二个&#34;如果&#34;条件缺少日期格式化
// if (! in_array( $day, $Kurstage))
if (! in_array( $day->format( "d.m.Y" ), $Kurstage))
答案 1 :(得分:0)
<?php
$weekday = 3;
$Kurstage = explode(", ","11.01.2017, 18.01.2017, 25.01.2017, 01.02.2017, 08.02.2017, 01.03.2017, 08.03.2017" );
$dateStart = date_create( "11.01.2017");
$dateEnd = date_create( "08.03.2017");
$missing_dates = array();
$interval = new DateInterval('P1D');
$period = new DatePeriod($dateStart, $interval, $dateEnd);
echo "dateStart:". $dateStart->format( "Y-m-d" ) . ", dateEnd: " .$dateEnd->format( "Y-m-d" )."\n";
foreach($period as $day)
{
if ( $day->format( "w" ) == $weekday )
{
if (! in_array( $day->format("d.m.Y"), $Kurstage))
{
echo $rowK->code. " - ". $day->format( "Y-m-d" ) ."\n";
}
}
}
?>