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

后端 未结 10 1740
终归单人心
终归单人心 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:27

    You use strtotime() and date():

    <?php
    $s = 'Monday, September 28, 2009';
    $time = strtotime($s);
    $start = strtotime('last sunday, 12pm', $time);
    $end = strtotime('next saturday, 11:59am', $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";
    ?>
    

    outputs:

    Input: Monday, September 28, 2009
    Output: Sunday, September 27, 2009 12:00 PM - Saturday, October 3, 2009 11:59 AM
    
    0 讨论(0)
  • 2020-12-15 12:28

    This is what your looking for. It is what I use.

    $beginningOfWeek = date('Y-m-d H:i:s', strtotime('last Sunday'));
    $endOfWeek = date('Y-m-d H:i:s', strtotime('+6 day', strtotime('last Sunday')) );
    
    0 讨论(0)
  • 2020-12-15 12:30

    strtotime

    $input = strtotime("Monday, September 28, 2009");
    $nextSunday = strtotime("next Sunday",$input);
    
    0 讨论(0)
  • 2020-12-15 12:31
    <?php
    
    $date = strtotime('Monday, September 28, 2009 - 1 day');
    $initialString =  date('l, F d, Y g:i A', $date);
    $end = date('l, F d, Y g:i A', strtotime( 'next saturday 11:59 pm', $date));
    
    echo $initialString . ' - ' . $end; 
    

    output: Sunday, September 27, 2009 12:00 AM - Saturday, October 03, 2009 11:59 PM

    0 讨论(0)
  • 2020-12-15 12:32
    <?php
    
    date_default_timezone_set('Europe/London');
    
    $datetime = new DateTime("2009-09-23 12:00:00");
    
    // Saturday
    $datetime->setISODate(2009, $datetime->format("W"), 6);
    print "Saturday:" . $datetime->format(DATE_ATOM) . "\n";
    // Sunday
    $datetime->setISODate(2009, $datetime->format("W"), 0);
    print "Sunday: " . $datetime->format(DATE_ATOM) . "\n";
    
    
    ?>
    
    0 讨论(0)
  • 2020-12-15 12:32

    echo date("Y-m-d H:i:s", strtotime("last sunday", time()));

    echo date("Y-m-d H:i:s", strtotime("next saturday", time()));

    0 讨论(0)
提交回复
热议问题