How to find the date of a day of the week from a date using PHP ?
How to find the date of a day of the week from a date using PHP ?
Here is the code, this works fine.
$dayofweek = date('w', strtotime($date)); $result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
We have to use the date() function through the following two ways.
date('w'); // day of week
or
date('l'); // dayname
Here is the example function for get the day nr,
function getWeekday($date) { return date('w', strtotime($date)); } echo getWeekday('2012-10-11'); // returns 4
One can use this code and find the date of a day of the week from a date.
$date = '2012-10-11'; $day = 1; $days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday','Friday', 'Saturday'); echo date('Y-m-d', strtotime($days[$day], strtotime($date)));