Reputation: 2921
How to get week start date Sunday and week end date Saturday in php.
I try my self below to code am getting Week Start Date as Monday Week End Date as Sunday.
$cdate = date("Y-m-d");
$week = date('W', strtotime($cdate));
$year = date('Y', strtotime($cdate));
$firstdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}+1"));
$lastdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
Thanks, Vasanth
Upvotes: 0
Views: 2245
Reputation: 3541
Here is a solution using DateTime
, which is a little bit nicer to work with than date
and strtotime
:) :
$today = new \DateTime();
$currentWeekDay = $today->format('w'); // Weekday as a number (0 = Sunday, 6 = Saturday)
$firstdayofweek = clone $today;
$lastdayofweek = clone $today;
if ($currentWeekDay !== '0') {
$firstdayofweek->modify('last sunday');
}
if ($currentWeekDay !== '6') {
$lastdayofweek->modify('next saturday');
}
echo $firstdayofweek->format('Y-m-d').PHP_EOL;
echo $lastdayofweek->format('Y-m-d').PHP_EOL;
Upvotes: 1
Reputation: 2921
I changed my code like below. This code is correct ? but it's worked for me.
$cdate = date("Y-m-d");
$week = date('W', strtotime($cdate));
$year = date('Y', strtotime($cdate));
echo "<br>".$firstdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-0"));
echo "<br>".$lastdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-6"));
Upvotes: 0
Reputation: 841
Change your local configuration :
setlocale('LC_TIME', 'fr_FR.utf8');
Change 'fr_FR.utf8' by your local.
Upvotes: 0