Reputation: 447
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.
I'have played around with the examples http://carbon.nesbot.com/docs/#api-getters but without success.
Any idea?
Upvotes: 27
Views: 91441
Reputation: 582
Worked fine for me.
$now = now();
$weekStartDate = $now->copy()->startOfWeek()->format('Y-m-d');
$weekEndDate = $now->copy()->endOfWeek()->format('Y-m-d');
Upvotes: 3
Reputation: 141
Please note, you will need to use CarbonImmutable if you want the Start Date to remain the beginning of the week. (Also it isn't "Laravel Carbon" it is just Carbon)
$now = CarbonImmutable::now();
$weekStartDate = $now->startOfWeek();
$weekEndDate = $now->endOfWeek();
Upvotes: 6
Reputation: 801
This is pretty simple with Carbon Library. Here is the code example:
$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
Upvotes: 65
Reputation: 579
The best way is using jquery plugin
In your view.blade.php make input field
<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
Upvotes: 8
Reputation: 390
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();
}
Upvotes: 5