PHP weekdays of current week, why is date() and strtotime taking next week?

陌路散爱 提交于 2019-12-05 18:52:46

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

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

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');
?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!