1

Possible Duplicate:
Get first day of week in PHP?

i have this script that give's me the beginning and the end of the current week:

$begweek = (date('l') == 'Monday')?date('Y-m-d'):date('Y-m-d',strtotime('last monday'));
echo $begweek;

echo "<br>";
$endweek = (date('l') == 'Sunday')?date('Y-m-d'):date('Y-m-d',strtotime('Sunday'));
echo $endweek;

what i need is a way to find the beginning and the end of any week if i pass a random date, like $date="2011-11-15";

an ideas on what i need to modify in the current script to accommodate this variable?

thanks

Community
  • 1
  • 1
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • 1
    see: http://stackoverflow.com/questions/6202576/get-all-work-days-in-a-week-for-a-given-date/6202709#6202709 - example: http://codepad.viper-7.com/82ADWj – Gordon Nov 22 '11 at 20:36
  • lots of duplicates: http://stackoverflow.com/search?q=first+day+of+week+php – Gordon Nov 22 '11 at 20:39

3 Answers3

3

The date function takes a second parameters.

THE CODE

function blah($date){

    $begweek = (date('l', strtotime($date)) == 'Monday')?date('Y-m-d', strtotime($date)):date('Y-m-d',strtotime('last monday', strtotime($date)));
    echo $begweek;

    echo "<br>";
    $endweek = (date('l', strtotime($date)) == 'Sunday')?date('Y-m-d', strtotime($date)):date('Y-m-d',strtotime('Sunday', strtotime($date)));
    echo $endweek;

}

blah("2011-11-15");

THE OUTPUT

2011-11-14
2011-11-20

Jose Vega
  • 10,128
  • 7
  • 40
  • 57
0

date takes a second parameter which is a timestamp to get the date of

use strtotime to get the timestamp.

date( 'l', strtotime( $date ) );
Galen
  • 29,976
  • 9
  • 71
  • 89
0

Use 'last monday', but just pass strtotime() the timestamp of the random date as it's second parameter.

Mario Lurig
  • 783
  • 1
  • 6
  • 16