How does PHP strtotime get the time of Monday? -1 Monday wrong.

Source: Internet
Author: User
Tags echo date
-1 Monday output is this Monday time, very strange

Reply content:

-1 Monday output is this Monday time, very strange

Because the foreigner thinks "this Monday" is next Monday, "Last Monday" is this Monday
Just "-2 Monday", or "Monday last week"?

Date (' y-m-d ', strtotime ('-'). (6+date (' W ')). ' days '));

-7 days

last week

Usually in two steps, get the last week of any day, and then go to the Monday, like the end of the month is to find the next 1th, and then 1 days.

Let's write a generic approach to this.

function last_monday($timestamp=0,$is_return_timestamp=true){     static $cache ;     $id = $timestamp.$is_return_timestamp;     if(!isset($cache[$id])){         if(!$timestamp) $timestamp = time();         $thismonday = this_monday($timestamp) - /*7*86400*/604800;         if($is_return_timestamp){             $cache[$id] = $thismonday;         }else{             $cache[$id] = date('Y-m-d',$thismonday);         }     }     return $cache[$id]; }

Date (' W ') gets the current week, since Monday to Saturday is 1-6, Sunday is 0. When the value was 0, the last Monday was 13 days ago. The rest is date (' W ') + 6 days ago.

$days = date('w')==0?13:date('w')+6;echo date('Y-m-d',time()-$days*86400);

Here you see the document link description

echo "Previous Monday:". Date ("Y-m-d", Strtotime ("Last Monday")). "
";

$time = Strtotime ("-7 Day");

$timetest =date ("y-m-d h:i:sa", $time);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.