Home >Backend Development >PHP Tutorial >How to get the first day of the week in PHP
strtotime() function: The strtotime() function returns the result in the timestamp by parsing the time string.
Syntax:strtotime($EnglishDateTime, $time_now)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 )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; ?>Output:
First day of this week: Monday - 11/02/2019In 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 $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; ?>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/2019Recommended: "
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!