4

With get the date of the first day of the week and last day of the week in php, first day Monday and the last day Friday, for example I have the date 2017-05-23 and I want to know the Monday one 2017-05-22 and the last one that would be 2017-05-26

date now = '2017-05-23'
first day of week = '2017-05-22' (Monday)
last day of week = '2017-05-26' (Friday)

Can I do this using date?

WMomesso
  • 244
  • 1
  • 4
  • 15
  • 1
    `$start = new DateTime('Last Week');` – Dimi May 23 '17 at 14:48
  • Possible duplicate of [How to find the day of week from a date using PHP?](https://stackoverflow.com/questions/12835133/how-to-find-the-day-of-week-from-a-date-using-php) – aynber May 23 '17 at 14:53

4 Answers4

9

Something like this:

<?php
date_default_timezone_set("Asia/Kolkata");
$today = date("Y-m-d");
$mon = new DateTime($today);
$sun = new DateTime($today);
$mon->modify('last Monday');
$sun->modify('next Sunday');
var_dump($mon);
var_dump($sun);
$first_day_of_week = $mon->format("Y-m-d");
$last_day_of_week = $sun->format("Y-m-d");
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
alanlittle
  • 460
  • 2
  • 12
  • 2
    If today were Monday and you ran `$mon->modify( 'Last Monday' );` would it show the previous week or remain the same? – Nathan Dawson May 23 '17 at 15:01
  • @NathanDawson remain the same. – WMomesso May 23 '17 at 15:07
  • 1
    @alanlittle $mon and $fri are the same? – WMomesso May 23 '17 at 16:37
  • 1
    @WagnerFernandoMomesso I'm not sure what you're asking. They are the same at first. When they are initialized, they have the date specified, but then `$mon` is modified to take the date of the previous Monday, and `$fri` is modified to take the date of the next Friday. – alanlittle May 23 '17 at 17:14
  • 2
    @alanlittle Now it worked, using so `$mon = $fri = new DateTime()`, did not work but then `$mon = new DateTime(); $fri = new DateTime()` worked, Thanks. – WMomesso May 23 '17 at 17:27
  • 1
    I tried this, passing "2018-01-01" (a Monday) and it returned 2017-12-25 instead. – Dario Fumagalli Oct 31 '18 at 01:26
  • 1
    Same, this does not appear to be working if today is Monday. Running ```->modify('Last Monday')``` on a Monday modifies the datetime to Monday of the previous week. – stevereds Dec 03 '20 at 09:04
  • @alanlittle `$mon` and `fri` are the same instance of `\DateTime` object. Its serious bug. See here: https://3v4l.org/3s9mN . You should change initialization of them as @WMomesso suggested above. Also you should start using `\DateTimeImmutable` instead to avoid such bugs. – domis86 May 05 '22 at 11:05
  • ok, now is better :) – domis86 Jan 04 '23 at 08:07
1
$the_date = '2017-05-23';
$the_day_of_week = date("w",strtotime($the_date)); //sunday is 0

$first_day_of_week = date("Y-m-d",strtotime( $the_date )-60*60*24*($the_day_of_week)+60*60*24*1 );
$last_day_of_week = date("Y-m-d",strtotime($first_day_of_week)+60*60*24*4 );

echo $first_day_of_week;
echo "~";
echo $last_day_of_week;
George Peter
  • 69
  • 1
  • 1
  • 6
0

there is a solution but I'm not sure if you will accept it like this

$First_date = date_create('this week')->format('Y-m-d H:i:s');
$Last_date = date_create('this week +4 days')->format('Y-m-d H:i:s');
echo $First_date;
echo $Last_date;
Omis Brown
  • 199
  • 2
  • 16
0

My solve,

$datenow = date('Y-m-d');// date now
$mon = new DateTime($datenow);
$fri = new DateTime($datenow);
$mon->modify('Last Monday');
$fri->modify('Next Friday');

using echo

echo 'Monday: '.$mon->format('Y-m-d').'<br>';
echo 'Friday: '.$fri->format('Y-m-d').'<br>';

using var_dump()

var_dump($mon);
var_dump($fri);
WMomesso
  • 244
  • 1
  • 4
  • 15