Get first/last day of week in php?

前端 未结 4 1287
说谎
说谎 2021-01-23 22:49

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 kn

相关标签:
4条回答
  • 2021-01-23 23:22

    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;
    
    0 讨论(0)
  • 2021-01-23 23:27
    $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;
    
    0 讨论(0)
  • 2021-01-23 23:30

    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);
    
    0 讨论(0)
  • 2021-01-23 23:39

    Something like this:

    $mon = $fri = new DateTime('2017-05-23');
    $mon->modify('Last Monday');
    $fri->modify('Next Friday');
    var_dump($mon);
    var_dump($fri);
    
    0 讨论(0)
提交回复
热议问题