3

I am using the following code to get a Y-m-d format of weekdays:

$monday = date('Y-m-d', strtotime('Monday'));
$tuesday = date('Y-m-d', strtotime('Tuesday'));
$wednesday = date('Y-m-d', strtotime('Wednesday'));
$thursday = date('Y-m-d', strtotime('Thursday'));
$friday = date('Y-m-d', strtotime('Friday'));

Today is 2012-08-01 (week 31) the Monday value I need should be 2012-07-30.

How come strtotime('Monday') makes it next monday?

David
  • 1,171
  • 8
  • 26
  • 48
  • Do you get correct results for all the days of the week which haven't passed already? The answer to that question is the answer to yours. – ChiefTwoPencils Aug 01 '12 at 09:31
  • The computer is trying to help you avoid headaches. If you used this code to get available dates for appointments, would you want people to schedule in the past. And really it has to make a choice, the Monday that just passed or next Monday; hmm!! You know what makes sense! – ChiefTwoPencils Aug 01 '12 at 09:39

3 Answers3

8

Because date('Y-m-d') returns today's date i.e.., month of august. And you are converting monday to time. now that time is represented in terms of date(Y-m-d) (august of 2012).. So the obvious answer would be the next coming monday starting from today.

So to get last week's date,use

$monday=date(Y-m-d,strtotime('monday this week'))
Bhuvan Rikka
  • 2,683
  • 1
  • 17
  • 27
  • 1
    Thank's for pointing that out. How would I do to get the date values of the current week - from mon to fri? – David Aug 01 '12 at 09:34
  • strtotime('Monday last week') should give this week's Monday. – irrelephant Aug 01 '12 at 09:38
  • You can do a `date_modify` as shown [here](http://www.php.net/manual/en/function.date-modify.php). It seems that you would need to provide some testing, you can't assume Monday has always already passed. So, you'd want to establish a start day for the week and `if` that date has passed `-x day`. – ChiefTwoPencils Aug 01 '12 at 09:45
  • But Monday this week and Monday last week can't be the same Monday! – irrelephant Aug 01 '12 at 09:53
  • :D yeah..so _this_ should be stressed ;) – Bhuvan Rikka Aug 01 '12 at 09:54
  • 1
    :O What kind of sorcery is this! That worked perfectly fine for me – Bhuvan Rikka Aug 01 '12 at 09:58
  • seriously dafuq! the same code returing two different outputs when run on two different platforms! the output on my localhost is showing `2012-07-30` and that website is showing `2012-08-06` – Bhuvan Rikka Aug 01 '12 at 10:02
  • @irrelephant will you please run that code on your localhost and report the output? – Bhuvan Rikka Aug 01 '12 at 10:03
  • @irrelephant Please check [this](http://stackoverflow.com/questions/2958327/get-date-of-monday-in-current-week-in-php-4). `(monday this week)` also works. – Bhuvan Rikka Aug 01 '12 at 10:12
  • 1
    @irrelephant ya 'this week' isn't working in ideone coz it is using php 5.2.12. date functions are modified in php 5.3 – Bhuvan Rikka Aug 03 '12 at 04:00
1

It alway return next day of the type. Next monday is 08-06, and next thursday is 08-02.

<?php
  function getDateOfWeekDay($day) {
    $weekDays = array(
      'Sunday',
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
    );

    $dayNumber = array_search($day, $weekDays);
    $currentDayNumber =  date('w', strtotime('today'));

    if ($dayNumber > $currentDayNumber) {
      return date('Y-m-d', strtotime($day));
    } else {
      return date('Y-m-d', strtotime($day) - 604800);
    }
  }

  echo  getDateOfWeekDay('Monday');
?>
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
1

For my application I needed to just create variables for the dates of the current week.

That's why I went along with this code:

$mon_value= date('Y-m-d', strtotime('Monday this week'));
$tue_value= date('Y-m-d', strtotime('Tuesday this week'));
$wed_value= date('Y-m-d', strtotime('Wednesday this week'));
$thu_value= date('Y-m-d', strtotime('Thursday this week'));
$fri_value= date('Y-m-d', strtotime('Friday this week'));
David
  • 1,171
  • 8
  • 26
  • 48