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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:53:57  来源:igfitidea点击:

Laravel Carbon get start + end of current week

phplaravellaravel-4php-carbon

提问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 上发布/安排餐厅菜单的工具。为此,我需要一个当前周的日期选择器,始终从星期一开始,始终在星期日结束。

Wireframe for restaurant menu

餐厅菜单的线框

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

Source: https://carbon.nesbot.com/docs/#api-getters

来源:https: //carbon.nesbot.com/docs/#api-getters

回答by Marko Milivojevic

The best way is using jquery plugin

最好的方法是使用 jquery 插件

http://api.jqueryui.com/datepicker/

http://api.jqueryui.com/datepicker/

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

这应该是这样的

http://imgur.com/K0ZhiVy

http://imgur.com/K0ZhiVy

回答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();
}