【问题标题】:Get whole week date starting from sunday to saturday from given date从给定日期获取从星期日到星期六的整个星期日期
【发布时间】:2018-06-12 22:23:10
【问题描述】:

大家好,我正在使用 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);

【问题讨论】:

  • 好的,所以您需要从 1 月 1 日到 1 月 7 日的周一到周日的日期?还是要求日期从 12 月 31 日到 1 月 6 日为周日到周六?
  • 请查看 DatePeriod 类。 link
  • @jilesh,从 12 月 31 日到 1 月 6 日,周日到周六

标签: php


【解决方案1】:

使用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>';

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

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>';

【讨论】:

  • 它没有按预期工作,如果我给 6,它会从给定日期开始 7 天,然后从 6 开始,但我希望给定日期的周日到周六意味着如果我给 6 它应该开始从 31 到 6,对于 7 - 7 到 13 类似,对于 8 - 7 到 13... 等等
  • 表示它应该从任何给定日期的星期日到星期六开始
  • 这应该是公认的答案。使用 php 的 Date 类是最常用的方法。您不需要编写自己的函数,这些函数与 php 的 Date 类完全相同。
【解决方案2】:

来自source

这是你要找的sn-p,

// 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";
}

这里正在工作demo

如果需要正常的标准格式代码,

这是你的sn-p,

// 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";
}

这里正在工作demo

编辑

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

<?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";
}

【讨论】:

  • 没用,周一到周日,但我想要周日到周六
  • @vasdevchandrakar 请检查我的编辑部分以满足您的要求
  • 乐于帮助@vasdevchandrakar
【解决方案3】:
<?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);





?>

【讨论】:

    【解决方案4】:

    试试这个:

    $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);
    

    【讨论】:

    • 抱歉没有意识到 W 返回“01”而不是 1...我的错,我更新了解决方案
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 2017-07-19
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多