How to find the date of a day of the week from a date using PHP?
If I've got a $date
YYYY-mm-dd
and want to get a specific $day
(specified by 0 (sunday) to 6 (saturday)) of the week that YYYY-mm-dd
is in.
For example, if I got 2012-10-11
as $date
and 5
as $day
, I want to get 2012-10-12
, if I've got 0
as $day
, 2012-10-14
EDIT:
Most of you misunderstood it. I got some date, $date
and want to get a day specified by 0-6 of the same week $date
is in.
So no, I don't want the day of $date
...
Solution 1:
I think this is what you want.
$dayofweek = date('w', strtotime($date));
$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
Solution 2:
You can use the date() function:
date('w'); // day of week
or
date('l'); // dayname
Example function to get the day nr.:
function getWeekday($date) {
return date('w', strtotime($date));
}
echo getWeekday('2012-10-11'); // returns 4