9

Possible Duplicate:
Get first day of week in PHP?

Given a timestamp I need to find the first day the week the timestamp belongs to.

e.g.

$format = "first day of week";

//convert to time
$time = strtotime("2011-07-01 00:00:00");

//format the time to the first day of the week
$date = strtotime($format,$time);

//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);

The week start should equal 2011-06-27 at the moment its gone horribly wrong and equals 1970-01-01 which suggests to me the “first day of week” format is invalid.

Any ideas would be much appreciated thanks,

Community
  • 1
  • 1
woot586
  • 3,906
  • 10
  • 32
  • 40

2 Answers2

12
$time = strtotime("2011-07-01 00:00:00");

$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
Ion Roata
  • 286
  • 3
  • 6
-1

Test it :

$time=[Your Request Time];

$dayofweek = date("w",strtotime($time));

if( $dayofweek != 0  )

    $firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );

else

    $firstdayOfWeek = strtotime($time);
Hamid
  • 1,700
  • 2
  • 13
  • 19