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,
To get the first day of the week, we subtract the day of the week from the day of the month. If the day of the week is Sunday, we subtract 6 to get Monday, if it is any other day we add 1 , because the getDay method returns a zero-based value.
Formula: =A2-WEEKDAY(A2,2)+1 Select a blank cell where you will return the beginning of week, and enter the formula =A2-WEEKDAY(A2,2)+1 (A2 is the cell with given date) into it, and drag the Fill Handle to the range as you need.
Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.
You can also use following lines of code to get first and last date of the week: var curr = new Date; var firstday = new Date(curr. setDate(curr. getDate() - curr.
$time = strtotime("2011-07-01 00:00:00");
$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With