122

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...

SparK
  • 5,181
  • 2
  • 23
  • 32
Zulakis
  • 7,859
  • 10
  • 42
  • 67
  • When does your week start? Monday or Sunday? I know it's Monday for most people but I'm just making sure :p – keyser Oct 11 '12 at 08:20
  • 4
    I don't think the people who answered understood the question properly. He does not want to get a number of the day by date (which would be date('w')), but he wants to specify date AND the day number and receive corresponding date from the same week as the specified date is... – Vafliik Oct 11 '12 at 08:30
  • The title made me think the misunderstanding too. I think it would be clearer with "as an offset" before "from" – zsalya Aug 07 '23 at 12:12

9 Answers9

166

I think this is what you want.

$dayofweek = date('w', strtotime($date));
$result    = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
Rezigned
  • 4,901
  • 1
  • 20
  • 18
131

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
powtac
  • 40,542
  • 28
  • 115
  • 170
20

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)));
air4x
  • 5,618
  • 1
  • 23
  • 36
  • 2
    Good approach, but as you can see in Rezigned's answer, you can also use '1 day', making the $days array useless. – Zulakis Oct 11 '12 at 08:55
  • 2
    This seem more correct because it returns the correct date `2012-10-14` when you input `0`, @Rezigned returns `2012-10-07` – Carl Sep 25 '19 at 06:17
11

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');
Johannes Buchholz
  • 1,857
  • 19
  • 34
5

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);

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
blue112
  • 52,634
  • 3
  • 45
  • 54
5

Just:

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

<?php
$day=5;
$w      = date("w", strtotime("2011-01-11")) + 1; // you must add 1 to for Sunday

echo $w;

$sunday = date("Y-m-d", strtotime("2011-01-11")-strtotime("+$w day"));

$result = date("Y-m-d", strtotime($sunday)+strtotime("+$day day"));

echo $result;

?>

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

lijinma
  • 2,914
  • 1
  • 23
  • 22
2

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

<?php
$scheduled_day = '2018-07-28';
$days = ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb'];
$day = date('w',strtotime($scheduled_day));
$scheduled_day = date('d-m-Y', strtotime($scheduled_day))." ($days[$day])";
// provides 28-07-2018 (Sáb)
victorf
  • 978
  • 2
  • 17
  • 35
1

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')
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
  • 1
    Thanks for your approach, but no, I don't have to do it manually. Have a look at Rezigned's answer. – Zulakis Oct 11 '12 at 08:51
1
<?php echo date("H:i", time()); ?>
<?php echo $days[date("l", time())] . date(", d.m.Y", time()); ?>

Simple, this should do the trick

Rune Bøge
  • 11
  • 1