## Use the PHP strtotime() function to get the first day of the week. This function returns the default time variable timestamp and then uses the date() function to convert the timestamp date into an understandable date.
strtotime() function: The strtotime() function returns the result in the timestamp by parsing the time string.
Syntax:
strtotime($EnglishDateTime, $time_now)
Copy after login
Parameters:
strtotime() function accepts the two parameters mentioned above, as described below:
$ EnglishDateTime: It Specify an English text date and time description indicating the date or time to be returned. This function parses the string and returns the time in seconds. This is a required parameter.
$ time_now: This parameter specifies the timestamp used to calculate the return value. This is an optional parameter.
date() function: The date() function returns a more understandable and human-readable date format.
Syntax:
date( format, timestamp )
Copy after login
Parameters: This function accepts the above two parameters, as described below:
format: Specifies the date and time format for displaying results.
timestamp: It is the default time variable for generating dates.
Note: In PHP, week starts on Monday, so if the time string is given as "this week", the output will be Monday's timestamp, which can be made by passing the date() function Readable.
Example 1: When the time string is "this week" (this week), get the default first day of the week.
<?php
$firstday = date('l - d/m/Y', strtotime("this week"));
echo "First day of this week: ", $firstday;
?>
Copy after login
Output:
First day of this week: Monday - 11/02/2019
Copy after login
In PHP, to use Sunday as the first day of the week, you need to consider the Sunday of the previous week. That is to say, to get the first day of a week (Sunday), you need to get the Sunday of the previous week, to get the first day of the next week (Sunday), you need to get the Sunday of this week, and so on.
PHP supports -ve indexing in time-string. So to get the previous week it can use the time string as "-1 week" and to get the current day it must also include the name of the datetime string.
Example 2: Get the first day of the week (Sunday).
<?php
$firstday = date('l - d/m/Y', strtotime("sunday -1 week"));
echo "First day of this week: ", $firstday, "\n";
$firstday = date('l - d/m/Y', strtotime("sunday -2 week"));
echo "First day of last week: ", $firstday, "\n";
$firstday = date('l - d/m/Y', strtotime("sunday 0 week"));
echo "First day of next week: ", $firstday, "\n";
$firstday = date('l - d/m/Y', strtotime("sunday 1 week"));
echo "First day of week after next week : ", $firstday;
?>
Copy after login
Output:
First day of this week: Sunday - 10/02/2019
First day of last week: Sunday - 03/02/2019
First day of next week: Sunday - 17/02/2019
First day of week after next week : Sunday - 24/02/2019
Copy after login
Recommended: "
PHP Tutorial"
This article is about how to get the first day of the week in PHP The method is introduced, I hope it will be helpful to friends who need it!
The above is the detailed content of How to get the first day of the week in PHP. For more information, please follow other related articles on the PHP Chinese website!