Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first day of week in PHP?

Tags:

php

datetime

Given a date MM-dd-yyyy format, can someone help me get the first day of the week?

like image 262
Iggy Ma Avatar asked Dec 13 '09 21:12

Iggy Ma


People also ask

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 do I print the day of the week in php?

If your date is already a DateTime or DateTimeImmutable you can use the format method. $day_of_week = intval($date_time->format('w'));

How can I get a week start and end in php?

getStartAndEndDate($week, $year); output: $return[0] = $firstDay; $return[1] = $lastDay; The return value will be something like an array in which the first entry is the week starting date and the second being the ending date.


1 Answers

Here is what I am using...

$day = date('w'); $week_start = date('m-d-Y', strtotime('-'.$day.' days')); $week_end = date('m-d-Y', strtotime('+'.(6-$day).' days')); 

$day contains a number from 0 to 6 representing the day of the week (Sunday = 0, Monday = 1, etc.).
$week_start contains the date for Sunday of the current week as mm-dd-yyyy.
$week_end contains the date for the Saturday of the current week as mm-dd-yyyy.

like image 54
JadedCore Avatar answered Sep 30 '22 16:09

JadedCore