Reputation: 165
I want to get the start date and end date of a week . I am using
$monday = date('Y-m-d', strtotime('this monday', strtotime($model->date)));
$saturday = date('Y-m-d', strtotime('this saturday', strtotime($model->date)));
when date is 18-09-2017 then monday is 18-09-2017 and saturaday is 23-09-2017. However, when date is 23-09-2017 then monday is 25-09-2017 and saturday is 23-09-2017.
Why is it so?
How can I adjust the code to give always moday as 18-09-2017 and saturday 23-09-2017 when the date is 18-09-2017 or 23-09-2017. I want uniformity.
Upvotes: 3
Views: 54
Reputation: 165
My code is
$dayofweek = date('w', strtotime($model->date));
if($dayofweek == 6)
{
$monday = date('Y-m-d', strtotime('last monday', strtotime($model->date)));
}
else
{
$monday = date('Y-m-d', strtotime('this monday', strtotime($model->date)));
}
$saturday = date('Y-m-d', strtotime('this saturday', strtotime($model->date)));
Upvotes: 1
Reputation: 16436
Try this date-time formatting function , convert it in ISO and get current week days
<?php
$dt = "23-09-2017";
function getDay($day,$date)
{
$days = ['Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 3, 'Thursday' => 4, 'Friday' => 5, 'Saturday' => 6, 'Sunday' => 7];
$date = new \DateTime($date);
$date->setISODate((int)$date->format('o'), (int)$date->format('W'), $days[ucfirst($day)]);
return $date;
}
echo "Monday: ".getDay('Monday',$dt)->format("d-m-Y");
echo "Saturday: ".getDay('Saturday',$dt)->format("d-m-Y");
Upvotes: 1