我用很长的时间缠着我的头,我想做的是获取本周的开始和结束日期。
本周从5月20日(星期一)至5月26日(星期日)开始
我的方法:
//GETTING DATA FOR CURRENT WEEK
$dtFrom_current = new DateTime; // get current date
$dtTo_current = new DateTime;
$dtFrom_current->setISODate($dtFrom_current->format('o'), $dtFrom_current->format('W'));
$dtTo_current->setISODate($dtTo_current->format('o'), $dtTo_current->format('W') );
// add 1 day
$dtTo_current->add(new DateInterval('P1D') + 1 );
// convert to iso date for database use
echo $dFrom_current = $dtFrom_current->format('Y-m-d');
echo $dTo_current = $dtTo_current->format('Y-m-d');
但我确定我做错了什么,但不知道该怎么做,我们将不胜感激。
您可能只是在这里丢失了一个小细节。
首先,DateInterval
应以间隔规范格式接收其参数。请参阅https://php.net/manual/en/dateinterval.construct.php。此外,您只需要将间隔增加1天,并根据要达到的目标增加6天:
$dtTo_current->add( new DateInterval( 'P6D' ) );
看一下重构的代码并对其进行测试:
//GETTING DATA FOR CURRENT WEEK
$dtFrom_current = new DateTime; // get current date
$dtTo_current = new DateTime;
$dtFrom_current->setISODate( $dtFrom_current->format( 'o' ), $dtFrom_current->format( 'W' ) );
$dtTo_current->setISODate( $dtTo_current->format( 'o' ), $dtTo_current->format( 'W' ) );
// add 1 day
$dtTo_current->add( new DateInterval( 'P6D' ) );
// convert to iso date for database use
echo $dFrom_current = $dtFrom_current->format( 'Y-m-d' );
echo "\n";
echo $dTo_current = $dtTo_current->format( 'Y-m-d' );
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句