0

I have a date 2015-12-16

i want to get the date of the first day's date for the current week of my date

here 2015-12-16 is in the week 51 of the year then i want to get the first day's date of the week 51 ( 2015-12-14 here)

how could i do it ? thank you

EDIT: it must work when there are 53 weeks in the year (like in 2015 for example)

RomMer
  • 909
  • 1
  • 8
  • 19
  • Could you share your code with us? – RobFos Apr 18 '17 at 14:05
  • 3
    [Google would've done the trick](http://stackoverflow.com/questions/1897727/get-first-day-of-week-in-php) – Thoby Apr 18 '17 at 14:06
  • @RobFos don't have working code – RomMer Apr 18 '17 at 14:08
  • @TV it's not what i want i want to do it with a given date – RomMer Apr 18 '17 at 14:09
  • 1
    Working code or not you should add it.. This is the point of SO rather than asking us to find you code or coding something up – Option Apr 18 '17 at 14:09
  • @Option i won't post code which do absolutely not what i want it won't help someone, including you. If i post this thread it's because i already tried but didn't succeed – RomMer Apr 18 '17 at 14:12
  • You've clearly missed the point of this place. With this being said I am leaving now, – Option Apr 18 '17 at 14:15
  • Possible duplicate of [How to get the first day of a given week number in PHP (multi-platform)?](http://stackoverflow.com/questions/1659551/how-to-get-the-first-day-of-a-given-week-number-in-php-multi-platform) – Matt S Apr 18 '17 at 19:40

3 Answers3

3

You can do the following:

 $dateTime = new DateTime("2015-12-16");

 $weekNo = $dateTime->format("W");

 $newDate = new DateTime();
 $newDate->setISODate($dateTime->format("Y"), $weekNo);

Example:

http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65

Since the above is a bit off in some cases here's something more reliable:

 $dateTime = new DateTime("2016-01-01");
 $dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D"));  //Since the weekdays are 1-based.

Example: http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851

apokryfos
  • 38,771
  • 9
  • 70
  • 114
3
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');

This gets start of this week if you count monday to sunday.

Marius Balčytis
  • 2,601
  • 20
  • 22
1

Try this:

date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));
tim
  • 2,530
  • 3
  • 26
  • 45