Change language

How do I get the first day of the week in PHP?

strtotime() Function:strtotime() function returns the result in a timestamp by parsing a time string. 
Syntax:
strtotime ($EnglishDateTime, $time_now) 
Parameters:The strtotime() function takes two parameter as above and described below:
  • $EnglishDateTime:specifies a textual description of the date and time in English that represents the date or time to be returned. The function parses the string and returns the time in seconds. This parameter is required.
  • $time_now:This parameter specifies the timelabel used to compute the return value. This is an optional parameter.
Functiondate(): The date() function returns a more human-readable date format. 
Syntax:
date (format, timestamp) 
Parameters:this function takes two parameters as specified above and described below:
  • Format:Specify the date and time format in which the result will be displayed.
  • Timestamp: is the default time variable from which the date will be generated.
Note.In PHP, the week starts on Monday, so if the time string is set as " this week ”, the result is a Monday timestamp that can be made readable by passing it through the date() function.Program 1:Get the first day of the week by default when the time string "This week".  
// PHP pro gram for printing by default
// day of the current week 
// I will show 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 ;  
?>
Exit:
First day of this week: Monday - 02/04/2019 
Sunday is generally considered the first day of the week. So, in PHP, to count Sunday as the first day of the week, consider the Sunday of the previous week. That is, to get the first day (Sunday) of the week, you need to get the Sunday of the previous week, and to get the first day (Sunday) of the next week, you need to get the Sunday of this week, and so on. 
PHP supports -ve indexing in temporary string. Thus, to get the previous week, he might use the time string as "-1 week", and to get the day, he might also need to include the name of the day in the time string.Program 2: Get the first day (Sunday) of the week.  
// PHP program to display Sunday as the first day of the week 
// I will show the name of the day
// d, m and Y will show the day, month and year, respectively 
// For the current week, the timeline will be & quot; Sunday -1 week & quot;
// here -1 stands for past 1st week $firstday = date ( ’l - d / m / Y’ , strtotime ( "sunday -1 week" )); echo "First day of this week:" , $firstday , " " ;  
// For the previous week, the timeline will be "Sunday -2 week" ;
// here -2 stands for the week before last week $firstday = date ( ’ l - d / m / Y’ , strtotime ( "sunday -2 week" )); echo "First day of last week:" , $firstday , " " ;  
// During the next week, the timeline will be "Sunday 0 week" ;
// here 0 stands for this week $firstday = date ( ’l - d / m / Y’ , strtotime ( "sunday 0 week" )); echo "First day of next week:" , $firstday , " " ;  
// During the week following the next week, the timeline will be & quot; Sunday 1 week & quot;
// here 1 indicates the next week $firstday = date ( ’l - d / m / Y’ , strtotime ( "sunday 1 week" )); echo "First day of week after next week:" , $firstday ;  
?>
Exit:
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 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method