How do i get the last day of the current week in php?

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?

asked May 23, 2017 at 14:46

2

Something like this:

$mon = $fri = new DateTime['2017-05-23'];
$mon->modify['Last Monday'];
$fri->modify['Next Friday'];
var_dump[$mon];
var_dump[$fri];

answered May 23, 2017 at 14:59

alanlittlealanlittle

4122 silver badges12 bronze badges

8

$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;

answered May 23, 2017 at 15:02

George PeterGeorge Peter

691 gold badge1 silver badge6 bronze badges

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;

answered May 23, 2017 at 15:01

Omis BrownOmis Brown

1992 silver badges16 bronze badges

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'].'
'; echo 'Friday: '.$fri->format['Y-m-d'].'
';

using var_dump[]

var_dump[$mon];
var_dump[$fri];

answered May 23, 2017 at 18:16

WMomessoWMomesso

2144 silver badges15 bronze badges

I'm adding a simple post here with a PHP method that has helped me. This method calculates the beginning and ending of a week given the year and week number. The problem I've run into is that “first day of the week” is subjective. Some people believe the first day of the week is “Monday” while others believe the first day of the week is “Sunday”. ISO-8601 specifies the first day of the week as “Monday”. Whereas, most western calendars display Sunday as the first day of the week and Saturday as the last day of the week.

To add to the confusion, PHP's methods themselves seem confused about what the first and last day of the week are.

For example:

$new_date = new DateTime;
// returns Monday, Jan 29 2018
$new_date->setISODate[2018, 5];

// returns Sunday, Feb 4 2018
$new_date->modify['sunday this week'];

// returns Sunday, Jan 28 2018
$new_date->setISODate[2018, 5 ,0];

You'll notice that the string "sunday this week" actually returns Sunday, Feb 4 whereas setting the date to the 0 day of the same week returns Sunday, Jan 28. I'm not saying that Sunday doesn’t happen twice a week… but Sunday doesn’t happen twice a week.

All this to say, the method below is the one I've found returns the most helpful results:

function get_first_and_last_day_of_week[ $year_number, $week_number ] {
    // we need to specify 'today' otherwise datetime constructor uses 'now' which includes current time
    $today = new DateTime[ 'today' ];

    return [object] [
        'first_day' => clone $today->setISODate[ $year_number, $week_number, 0 ],
        'last_day'  => clone $today->setISODate[ $year_number, $week_number, 6 ]
    ];
}

Cross Posting from //jeremysawesome.com/2019/08/12/php-get-first-and-last-day-of-week-by-week-number/

How do I get the day of the week in 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 can I get first and last date of current week in PHP?

$monday = date['d-m-Y',strtotime['last monday',strtotime['next monday',strtotime[$date]]]]; You have to get next monday first then get the 'last monday' of next monday. So if the given date is monday it will return the same date not last week monday.

How do I get the last day of the current month in PHP?

$date = strtotime [ $datestring ]; // Last date of current month. $day = date [ "l" , $lastdate ];

How can I get last Monday date in PHP?

Just use the date relative format: $date = new DateTime["last monday"];

Chủ Đề