PHP: How to get Sunday and Saturday given a date input?

后端 未结 10 1741
终归单人心
终归单人心 2020-12-15 12:15

How can I get the Sunday and Saturday of the week given a specific date?

For example:

input: Monday, September 28, 2009

output should be:

相关标签:
10条回答
  • 2020-12-15 12:37

    You may find my Date Time Helper much handy in this case , it can finish in 3 Lines

    http://normandqq.github.io/Date-Time-Helper/

        $date = '2013-08-11';
        $monday = Model_DTHpr::getMondayByDate($date);
        $sunday = Model_DTHpr::adjustDays("add",$monday,6);
    

    Out Put:

        2013-08-05 //Monday
        2013-08-11 //Sunday
    
    0 讨论(0)
  • 2020-12-15 12:48

    Get Sunday Php Code:

    echo "Today Is : " . date("d-m-Y"); 
    $dw = date( "w");
    $dw = 7-$dw;
    echo "\nNext Sunday Is" .date('d-m-Y', strtotime("+$dw days")); 
    
    0 讨论(0)
  • 2020-12-15 12:50

    take a look at strtotime

    i.e.

    strtotime('next sunday', strtotime($your_date_string));
    
    0 讨论(0)
  • 2020-12-15 12:52

    the issue with the 'correct' answer is, what if the supplied date is Sunday? Then it will give you the previous Sunday, instead of the current Sunday. Same issue with Saturday.

    $s = 'Monday, September 28, 2009';
    $time = strtotime($s);
    if(date('D', $time) == "Sun"){
        $start = strtotime(' 12pm', $time);
    }
    else {
        $start = strtotime('last sunday, 12pm', $time);
    }
    if(date('D', $time) == "Sat"){
        $start = strtotime(' 12pm', $time);
    }
    else {
        $start = strtotime('next saturday, 12pm', $time);
    }
    $format = 'l, F j, Y g:i A';
    $start_day = date($format, $start);
    $end_day = date($format, $end);
    header('Content-Type: text/plain');
    echo "Input: $s\nOutput: $start_day - $end_day";
    
    0 讨论(0)
提交回复
热议问题