How to get first day of week php?

Very simple to use strtotime function:

echo date("Y-m-d", strtotime('monday this week')), "\n";   

echo date("Y-m-d", strtotime('sunday this week')), "\n";

It differs a bit across PHP versions:

Output for 5.3.0 - 5.6.6, php7@20140507 - 20150301, hhvm-3.3.1 - 3.5.1

2015-03-16
2015-03-22

Output for 4.3.5 - 5.2.17

2015-03-23
2015-03-22

Output for 4.3.0 - 4.3.4

2015-03-30
2015-03-29

Comparing at Edge-Cases

Relative descriptions like this week have their own context. The following shows the output for this week monday and sunday when it's a monday or a sunday:

$date = '2015-03-16'; // monday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";   
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";

$date = '2015-03-22'; // sunday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";   
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";

Againt it differs a bit across PHP versions:

Output for 5.3.0 - 5.6.6, php7@20140507 - 20150301, hhvm-3.3.1 - 3.5.1

2015-03-16
2015-03-22
2015-03-23
2015-03-29

Output for 4.3.5 - 5.0.5, 5.2.0 - 5.2.17

2015-03-16
2015-03-22
2015-03-23
2015-03-22

Output for 5.1.0 - 5.1.6

2015-03-23
2015-03-22
2015-03-23
2015-03-29

Output for 4.3.0 - 4.3.4

2015-03-23
2015-03-29
2015-03-30
2015-03-29

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date.

strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.
Syntax:

strtotime($EnglishDateTime, $time_now)

Parameters: The strtotime() function accepts two parameters as mentioned above and described below:

  • $EnglishDateTime: It specifies the English textual date-time description, which represents the date or time to be returned. The function parses the string and returns the time in seconds. It is required parameter.
  • $time_now: This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.

date() Function: The date() function returns more understandable and human readable format of date.
Syntax:

date( format, timestamp )

Parameters: This function accepts two parameters as mentioned above and described below:

  • format: It specify the format for date and time in which the result will be display.
  • timestamp: It is the default time variable from which the date will be generated.

Note: In PHP, the week starts with Monday, so if the time-string is given as “this week” the output will be timestamp of Monday which can make readable by passing it through date() function.

Program 1: Get the default first day of a week when the time-string is “this 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 - 04/02/2019

Now, Usually Sunday is considered as the first day of a week. So, in PHP to get the Sunday of as first day of a week, consider the Sunday of previous week. That is to get the first day (Sunday) of a week need to get the Sunday of previous week and to get the first day (Sunday) of next week need to get the Sunday of this week and so on.
PHP supports -ve indexing in time-string. So, in order to get the previous week it can use the time string as “-1 week” and to get the day it can also have to include the name of the day in time string.

Program 2: Get the first day (Sunday) of the week.

<?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 - 03/02/2019
First day of last week: Sunday - 27/01/2019
First day of next week: Sunday - 10/02/2019
First day of week after next week : Sunday - 17/02/2019


How do I get the start of the week in PHP?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.

How do you determine the first day of the week?

To get the first day of the week, we subtract the day of the week from the day of the month. If the day of the week is Sunday, we subtract 6 to get Monday, if it is any other day we add 1 , because the getDay method returns a zero-based value.

How can I get first and last date of current week in PHP?

$monday = date('d-m-Y',strtotime('last monday',strtotime('next monday',strtotime($date)))); You have to get next monday first then get the 'last monday' of next monday. So if the given date is monday it will return the same date not last week monday.

How do I get the last day of the current week in PHP?

Get first/last day of week in php?.
$start = new DateTime('Last Week'); – Dimi. May 23, 2017 at 14:48..
Possible duplicate of How to find the day of week from a date using PHP? – aynber. May 23, 2017 at 14:53..