php Laravel Carbon 开始 + 本周结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36719453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Carbon get start + end of current week
提问by Steve Brown
I am working with Laravel 4 on a tool to publish/schedule restaurant menus on facebook. For this I need a date selector for the current week, starting always on monday and ending always on sunday.
我正在与 Laravel 4 合作开发一种在 facebook 上发布/安排餐厅菜单的工具。为此,我需要一个当前周的日期选择器,始终从星期一开始,始终在星期日结束。
I'have played around with the examples http://carbon.nesbot.com/docs/#api-gettersbut without success.
我玩过示例http://carbon.nesbot.com/docs/#api-getters但没有成功。
Any idea?
任何的想法?
回答by Its_aggarwal
This is pretty simple with Carbon Library. Here is the code example:
这对于 Carbon Library 来说非常简单。下面是代码示例:
$now = Carbon::now();
$weekStartDate = $now->startOfWeek()->format('Y-m-d H:i');
$weekEndDate = $now->endOfWeek()->format('Y-m-d H:i');
Even you have the option to change start and end day of the week. It is like this,
即使您可以选择更改一周的开始和结束日期。是这样的,
$start = $now->startOfWeek(Carbon::TUESDAY);
$end = $now->endOfWeek(Carbon::MONDAY);
回答by Marko Milivojevic
The best way is using jquery plugin
最好的方法是使用 jquery 插件
In your view.blade.php make input field
在您的 view.blade.php 制作输入字段
<input type="text" id="in">
In your script file select this input and set date range
在您的脚本文件中选择此输入并设置日期范围
<script>
$("#in").datepicker({
minDate: new Date("{{Carbon\Carbon::now()->startOfWeek()->format('Y/m/d')}}"),
maxDate: new Date("{{Carbon\Carbon::now()->endOfWeek()->format('Y/m/d')}}")
});
</script>
This should be look like this
这应该是这样的
回答by Demian
This gives you the start of the week (monday) until the end of the week (sunday).
No idea whether this is a setting on the server. (Some people put the initial week on Sunday)
这为您提供了一周的开始(星期一)到一周的结束(星期日)。
不知道这是否是服务器上的设置。(有些人把最初的一周放在周日)
private $start;
private $end;
public function setWeekPeriod($weeknumber)
{
$week_start = (new DateTime())->setISODate(date("Y"),$weeknumber)->format("Y-m-d H:i:s");
$this->start = Carbon::createFromFormat("Y-m-d H:i:s", $week_start);
$this->start->hour(0)->minute(0)->second(0);
$this->end = $this->start->copy()->endOfWeek();
}