How to get day of the week in php?

If I've got a $date YYYY-mm-dd and want to get a specific $day (specified by 0 (sunday) to 6 (saturday)) of the week that YYYY-mm-dd is in.

For example, if I got 2012-10-11 as $date and 5 as $day, I want to get 2012-10-12, if I've got 0 as $day, 2012-10-14

EDIT:
Most of you misunderstood it. I got some date, $date and want to get a day specified by 0-6 of the same week $date is in.

So no, I don't want the day of $date...

How to get day of the week in php?

SparK

5,1332 gold badges23 silver badges32 bronze badges

asked Oct 11, 2012 at 8:17

2

I think this is what you want.

$dayofweek = date('w', strtotime($date));
$result    = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));

answered Oct 11, 2012 at 8:35

RezignedRezigned

4,7831 gold badge19 silver badges18 bronze badges

0

You can use the date() function:

date('w'); // day of week

or

date('l'); // dayname

Example function to get the day nr.:

function getWeekday($date) {
    return date('w', strtotime($date));
}

echo getWeekday('2012-10-11'); // returns 4

answered Oct 11, 2012 at 8:20

powtacpowtac

39.8k27 gold badges115 silver badges167 bronze badges

Try

$date = '2012-10-11';
$day  = 1;
$days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday','Friday', 'Saturday');
echo date('Y-m-d', strtotime($days[$day], strtotime($date)));

answered Oct 11, 2012 at 8:30

air4xair4x

5,5601 gold badge22 silver badges36 bronze badges

2

If your date is already a DateTime or DateTimeImmutable you can use the format method.

$day_of_week = intval($date_time->format('w'));

The format string is identical to the one used by the date function.


To answer the intended question:

$date_time->modify($target_day_of_week - $day_of_week . ' days');

answered Sep 17, 2018 at 8:46

PHP Manual said :

w Numeric representation of the day of the week

You can therefore construct a date with mktime, and use in it date("w", $yourTime);

How to get day of the week in php?

answered Oct 11, 2012 at 8:21

blue112blue112

48.6k3 gold badges45 silver badges54 bronze badges

0

Just:

2012-10-11 as $date and 5 as $day


The $result = '2012-10-12' is what you want.

answered Oct 11, 2012 at 8:32

lijinmalijinma

2,8241 gold badge23 silver badges22 bronze badges

I'm afraid you have to do it manually. Get the date's current day of week, calculate the offset and add the offset to the date.

$current = date("w", $date)
$offset = $day - $current
$new_date = new DateTime($date)
    ->add(
        new DateInterval($offset."D")
    )->format('Y-m-d')

answered Oct 11, 2012 at 8:29

How to get day of the week in php?

John DvorakJohn Dvorak

26.2k12 gold badges68 silver badges83 bronze badges

1

I had to use a similar solution for Portuguese (Brazil):

answered Jul 19, 2018 at 19:00

How to get day of the week in php?

victorfvictorf

9092 gold badges17 silver badges33 bronze badges



Simple, this should do the trick

answered Jan 18, 2019 at 15:10

How do I get the day 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 can I get the day of a specific date with PHP?

Whenever you need to det day like Monday, Tuesday etc from full date. i mean you have any specific date like "2015-10-10" and you want to get day then you can do in both way first using strtotime() and second using DateTime object.

How can I get first day of next week in PHP?

How to get first day of week in PHP?.
Example. Live Demo..
Output. This will produce the following output − First day = Monday - 09/12/2019..
Example. Let us now see another example −.
Output. This will produce the following output − Displaying Sunday as first day of a week... First day (next week) = Sunday - 15/12/2019..

How can I tell if PHP is today is Monday?

This should solve it: $day = date('D'); $date = date('d') if($day == Mon){ //Code for monday } if($date == 01){ //code for 1st fo the month } else{ //not the first, no money for you =/ }