通过提供月份和日期的周数来获取确切的日期?

时间:2013-09-19 09:30:37

标签: php date

是否有任何PHP功能,我可以通过提供月份和日期的周数来获得确切的日期,

function required_date($week_num, $day) {
// should return 20th of September, if i pass (3, 'Friday')
} 

我将获得当月的周数

ceil(substr(date('Y-m-d'), -2) / 7);

和白天,

date('l');

请帮忙!

3 个答案:

答案 0 :(得分:0)

试试这个:

date('Y-m-d', strtotime('W' . $week_num . ' ' . $day));

答案 1 :(得分:0)

怎么样

function required_date($week_num, $day) 
{
    return date('Y-m-') . substr('0' . (($week_num - 1) * 7 + date('N', strtotime($day))) + 1, -2);
}

var_dump(required_date(3, 'Friday'));
var_dump(required_date(4, 'Friday'));

答案 2 :(得分:0)

function required_date($week_num, $day) {
  $week_of_year = sprintf('%02d', date('W', strtotime(date('Y-m-01'))) + $week_num);
  $day_of_week  = date('N', strtotime($day));
  $timestamp    = strtotime(date('Y') . '-W' . $week_of_year . '-' . $day_of_week);

  return $timestamp;
}


$timestamp = required_date(3, 'friday');
echo date('Y-m-d', $timestamp);