从给定日期开始从星期日到星期六获取整个星期的日期



大家好,我正在使用php,我的要求是从给定日期获取完整的周日期,因为我需要计算每周工作时间。一周必须从周日到周六开始,而不是周一到周日。我有代码可以在除星期日以外的一周中的其他日子正常工作。这意味着如果给出周一到周六的任何日期,它可以正常工作,但如果我给出周日的日期,它会给出上周的日期。请检查我的代码并建议我提供更好的解决方案。

$days = array();                
$ddate = "2018-01-07";
$date = new DateTime($ddate);
$week = $date->format("W");
$y =    date("Y", strtotime($ddate));
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";             
for($day=0; $day<=6; $day++)
{
    $days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}               
print_r($days);

使用 DateTimeDateIntervalDatePeriod 类,你也许可以这样做

function getperiod( $start ){
    return new DatePeriod(
        new DateTime( $start ),
        new DateInterval('P1D'),
        new DateTime( date( DATE_COOKIE, strtotime( $start . '+ 7days' ) ) )
    );
}
$start='2018-01-07';
$period=getperiod( $start );
foreach( $period as $date ){
    echo $date->format('l -> Y-m-d') . '<br />';
}

哪个返回

Sunday -> 2018-01-07
Monday -> 2018-01-08
Tuesday -> 2018-01-09
Wednesday -> 2018-01-10
Thursday -> 2018-01-11
Friday -> 2018-01-12
Saturday -> 2018-01-13

或者,通过修改getperiod函数的参数,可以使该函数更加灵活。

function getperiod( $start, $interval='P1D', $days=7 ){
    return new DatePeriod(
        new DateTime( $start ),
        new DateInterval( $interval ),
        new DateTime( date( DATE_COOKIE, strtotime( $start . '+ '.$days.' days' ) ) )
    );
}
$start='2018-01-07';
$days=array();
$period=getperiod( $start );
foreach( $period as $date ){
    $days[]=$date->format('Y-m-d');
}
echo '<pre>',print_r($days,true),'</pre>';

例如:找到下一年的每个星期日

$period=getperiod( $start,'P7D', 365 );
foreach( $period as $date ){
    $days[]=$date->format('Y-m-d');
}
echo '<pre>',print_r($days,true),'</pre>';

确保计算从数值为 7Sunday开始

function getperiod( $start, $interval='P1D', $days=7 ){
    return new DatePeriod(
        new DateTime( $start ),
        new DateInterval( $interval ),
        new DateTime( date( DATE_COOKIE, strtotime( $start . '+ '.$days.' days' ) ) )
    );
}
/* A date from which to begin calculations */
$start='2018-01-01';
/* Array to store output */
$days=array();
/* integer to represent which day of the week to operate upon */
$startday = 7;
/* Output format for resultant dates */
$output='Y-m-d';
/* Calculate initial startdate given above variables */
$start=date( DATE_COOKIE, strtotime( $start . ' + ' . ( $startday - date( 'N', strtotime( $start ) ) ) . ' days' ) );
/* Get the period range */
$period=getperiod( $start );
foreach( $period as $date ){
    /* store output in desired format */
    $days[]=$date->format( $output );
}
/* do something with data */
echo '<pre>',print_r($days,true),'</pre>';

从源代码,

这是您正在寻找的代码片段,

// set current date
$date = '01/03/2018';
// parse about any English textual datetime description into a Unix timestamp 
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday 
for ($i = 0; $i < 7; $i++, $ts += 86400){
    print date("m/d/Y l", $ts) . "n";
}

这是工作演示。

如果您需要普通的标准格式代码,

这是你的片段,

// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow    = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset * 86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
    print date("Y-m-d l", $ts) . "n";
}

这是工作演示。

编辑

根据您的要求,现在一周将从周日到周六开始

<?php
// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Sunday
$dow    = date('w', $ts);
$offset = $dow;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Sunday
$ts = $ts - $offset * 86400;
// loop from Sunday till Saturday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
    print date("Y-m-d l", $ts) . "n";
}
<?php
$days = array();                
$ddate = "2018-01-07";
$y =    date("Y", strtotime($ddate));
if(date("l", strtotime($ddate))=='Sunday'){
    $ddate = date("Y-m-d ", strtotime($ddate. "+1 day"));
}
$date = new DateTime($ddate);
$week = $date->format("W");
echo "<br/>";
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";             
for($day=0; $day<=6; $day++)
{
    $days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}               
print_r($days);


?>

试试这个:

$days = array();                
$ddate = "2018-01-07";
$date = new DateTime($ddate);
$week = $date->format("N")==7?$date->modify("+1 week")->format("W"):$date->format("W");
$y =    date("Y", strtotime($ddate));
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";             
for($day=0; $day<=6; $day++)
{
    $days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}               
print_r($days);
$dto = new DateTime();
$year = date_create($this->week_date)->format('o');
$week_no = date('W', strtotime($this->week_date));
$dto->setISODate($year, $week_no);
$dto->modify('-1 days');
$ret['sunday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['monday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['tuesday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['wednesday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['thursday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['friday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['saturday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['next_sunday'] = $dto->format('Y-m-d');

最新更新