Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the first day of the week a given date belongs to? [duplicate]

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,

like image 872
woot586 Avatar asked Jul 01 '11 08:07

woot586


People also ask

How do you determine the first day of the week?

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.

How do you find the first day of the week in Excel?

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.

How do you find the date of a day of the week from a date using PHP?

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.

How do you get the current week start date and end date in typescript?

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.


1 Answers

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

$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));
like image 144
Ion Roata Avatar answered Sep 20 '22 11:09

Ion Roata