PHP gets the start and end dates for this week and last week

Source: Internet
Author: User

Recently, I used PHP to get the current week and last week's start date and end date because of the work. Search the Internet, there is no suitable, so I did a summary. The specific content and code are as follows.

<?php
header (' content-type:text/html; Charset=utf-8 ');

$date =date (' y-m-d ');  The current date

$first =1;//$first =1 indicates that weekly Monday is the start date 0 for each Sunday as the start date

$w =date (' W ', Strtotime ($date));  Gets the day of the current week Sunday is 0 weeks one to Saturday is 1-6 

$now _start=date (' y-m-d ', Strtotime ("$date-". $w? $w-$first: 6). ' Days '); Get the start date for this week, if $w is 0, Sunday, minus 6 days

$now _end=date (' y-m-d ', Strtotime ("$now _start +6"));  This week's end date

$last _start=date (' y-m-d ', Strtotime ("$now _start-7 Days"));  Last week's start date

$last _end=date (' y-m-d ', Strtotime ("$now _start-1 Days"));  Last week's end date

Echo ' This week's start date: ', $now _start, ' <br/> ';
Echo ' This week's End date: ', $now _end, ' <br/> ';
Echo ' last week's start date: ', $last _start, ' <br/> ';
Echo ' Last week's End date: ', $last _end, ' <br/> ';

Note: Since the foreign week is from Sunday onwards, China's custom from Monday onwards, so the direct use of Strtotime ("last Monday") will have the wrong results.

Articles that you may be interested in

    • How PHP Gets the start time stamp and end time stamp for today, yesterday, last week, month
    • PHP gets the timestamp of the start and end times of the week that the specified date is in
    • PHP gets a list of dates 30 days before the current date
    • PHP calculates the function of how many years, how many months, how many days apart two dates
    • PHP in front of quotes to add the reason for the backslash and PHP to remove the backslash, three ways to turn off the PHP magic quotes
    • PHP extracts the birthday date in the ID number and the function to verify that it is a minor
    • PHP detects whether the server SSL is turned on and how to turn on SSL
    • PHP to determine the most secure upload file type, the most realistic solution


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.