How to get first day of 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.
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”.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to print default first
// day of current week
  
// l will display the name of the day
  
// d, m, Y will display the day, month
// and year respectively 
$firstday = date('l - d/m/Y', strtotime("this week"));
  
echo "First day of this week: ", $firstday;
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to display sunday as first day of a week
  
// l will display the name of the day
// d, m and Y will display the day, month and year respectively
  
// For current week the time-string will be "sunday -1 week"
// here -1 denotes last week
$firstday = date('l - d/m/Y', strtotime("sunday -1 week"));
echo "First day of this week: ", $firstday, "\n";
  
// For previous week the time-string wil be "sunday -2 week"
// here -2 denotes week before last week
$firstday = date('l - d/m/Y', strtotime("sunday -2 week"));
echo "First day of last week: ", $firstday, "\n";
  
// For next week the time-string wil be "sunday 0 week"
// here 0 denotes this week
$firstday = date('l - d/m/Y', strtotime("sunday 0 week"));
echo "First day of next week: ", $firstday, "\n";
  
// For week after next week the time-string wil be "sunday 1 week"
// here 1 denotes next week
$firstday = date('l - d/m/Y', strtotime("sunday 1 week"));
echo "First day of week after next week : ", $firstday;
  
?>

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.