简体   繁体   中英

Get exact date by providing week number of month and day?

Is there any php function where i can get exact date, by providing week number of the month and day as,

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

I am getting week number of the month as

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

and day as,

date('l');

Any help, please!

尝试这个:

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

How about

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM