lovespring
lovespring

Reputation: 19589

How to get weekend day in this week by any given date?

for example, get the weekend of today.

Upvotes: 2

Views: 4429

Answers (5)

nikc.org
nikc.org

Reputation: 16993

If you're looking for e.g. the next or previous saturday (or any other weekday for that matter) strtotime is your friend.

$prev = strtotime('-1 saturday');    
$next = strtotime('saturday');

var_dump($prev, $next);

It's worth noting that strtotime is quite an expensive function, so multiple calculations will noticiably add to your execution time. A good compromise is using it to get a starting point and using the other date functions to derive further dates.

Upvotes: 4

Kashyap Nadig
Kashyap Nadig

Reputation: 116

I think the built-in PHP function strtotime should work for you. Eg:

strtotime('next saturday')

http://php.net/manual/en/function.strtotime.php

Upvotes: 0

Kalessin
Kalessin

Reputation: 2302

This is brilliantly easy in PHP:

<?php

echo date( 'Y-m-d', strtotime( 'next Saturday' ) );

?>

Upvotes: 3

Nanne
Nanne

Reputation: 64439

Your question is a bit a hard to follow, but do you mean "the next weekend" from a certain date?

You could get the the weekday number, and then see how much to add for saturday and sundag? That would look like:

<?php   
    $dayNR = date('N');         //monday = 1, tuesday = 2, etc.
    $satDiff = 6-$dayNR;        //for monday we need to add 5 days -> 6 - 1
    $sunDiff = $satDiff+1;      //sunday is one day more
    $satDate = date("Y-m-d", strtotime(" +".$satDiff." days"));
    $sunDate = date("Y-m-d", strtotime(" +".$sunDiff." days"));

    echo $satDate."\n";
    echo $sunDate."\n";
?>

Upvotes: 1

Stefan Steiger
Stefan Steiger

Reputation: 82406

get the current date.
get the current day of week. (0=monday, 6 = sunday)
days2weekend = 5 - current day of week 
dateadd(currentdate, days, days2weekend)

Upvotes: 1

Related Questions