首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一周一天从太阳到特定的一周

一周一天从太阳到特定的一周
EN

Stack Overflow用户
提问于 2020-01-19 19:30:56
回答 2查看 52关注 0票数 0

我有一个从输入周开始的一周格式,如

$the_week =2020年-W01

我怎样才能让所有的工作日从太阳到这个特定的一周,在PHP?编辑

代码语言:javascript
复制
 $first_date =  date('Y-m-d',strtotime("W".$the_week)); //first date
 $d_1 = date("Y-m-d",strtotime("+1 day", strtotime($first_date)));
 $d_2 = date("Y-m-d",strtotime("+2 day", strtotime($first_date)));
 $d_3 = date("Y-m-d",strtotime("+3 day", strtotime($first_date)));
 $d_4 = date("Y-m-d",strtotime("+4 day", strtotime($first_date)));
 $d_5 = date("Y-m-d",strtotime("+5 day", strtotime($first_date)));
 $d_6 = date("Y-m-d",strtotime("+6 day", strtotime($first_date)));

我得到了1999年

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-19 19:41:32

您可以使用DateTime API,获取给定的日期,然后使用DateTime#add和适当的DateInterval累计6天。

代码语言:javascript
复制
$baseDay = new \DateTimeImmutable($the_week . ' -1day');

for ($dayIncrement = 0; $dayIncrement < 7; $dayIncrement++) {
  $day = $baseDay->add(new \DateInterval("P{$dayIncrement}D"));
  echo $day->format('d/m/Y'), PHP_EOL;
}

演示:

票数 1
EN

Stack Overflow用户

发布于 2020-01-20 08:42:49

下面的代码运行良好

代码语言:javascript
复制
<?php
$first_date = date('Y-m-d',strtotime("W2020-W01"));
$d_1 = date("Y-m-d",strtotime("+1 day", strtotime($first_date)));
$d_2 = date("Y-m-d",strtotime("+2 day", strtotime($first_date)));
$d_3 = date("Y-m-d",strtotime("+3 day", strtotime($first_date)));
$d_4 = date("Y-m-d",strtotime("+4 day", strtotime($first_date)));
$d_5 = date("Y-m-d",strtotime("+5 day", strtotime($first_date)));
$d_6 = date("Y-m-d",strtotime("+6 day", strtotime($first_date)));
echo $d_1;
echo $d_2;
echo $d_3;
echo $d_4;
echo $d_5;
echo $d_6;
?>

有什么问题吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59813646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档