Carbon:在知道一年和一年中的一周时获取一周的开始和结束日期

编程入门 行业动态 更新时间:2024-10-23 07:24:36
本文介绍了Carbon:在知道一年和一年中的一周时获取一周的开始和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Carbon 提供了函数 weekOfYear 来获取一年中的第几周作为整数.但是,我需要反过来以根据年份 + 一年中的一周来获取日期.

Carbon provides the function weekOfYear to get the week of the year as integer. However I need to go the other way round to get the a date based on the year + the week of the year.

Carbon::now()->weekOfYear(); // todays week of the year

例如

  • 年份:2016
  • 一年中的第 42 周

因此,我需要这一周的开始和结束日期.但是我在 Carbon 文档中找不到合适的函数

As a result i need the start and end date of this given week. However i cannot find a fitting function in the Carbon docs

推荐答案

Carbon 是 PHP 的 DateTime 的包装器,因此您可以使用 setISODate:

Carbon is a wrapper for PHP's DateTime, so you can use setISODate:

$date = Carbon::now(); // or $date = new Carbon(); $date->setISODate(2016,42); // 2016-10-17 23:59:59.000000 echo $date->startOfWeek(); // 2016-10-17 00:00:00.000000 echo $date->endOfWeek(); // 2016-10-23 23:59:59.000000

更多推荐

Carbon:在知道一年和一年中的一周时获取一周的开始和结束日期

本文发布于:2023-10-28 23:28:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:年中   日期   结束   Carbon

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!