简体   繁体   中英

Get date by passing week number of month and day using loop?

I am little worried about to get date from a function by providing week number of a month and day for multiple months,

Like,

$date = new DateTime();
        $event->event_date = required_date($event->monthly_recurring_week, $event->monthly_recurring_day, $date->format('Y-m-01'));
        if($event->event_date < $event->recurring_end_date) { // check for wrong date or not exist event
        //problem in this code for next months wrong date
           for($event->event_date; $event->event_date < $event->recurring_end_date;){
               $event->event_date = $event->event_date;
               $events[] = (array) $event;
               $date->modify('Next month');
               $event->event_date = required_date($event->monthly_recurring_week, $event->monthly_recurring_day, $date->format('Y-m-01'));
           }
        }
function required_date($week_num, $day, $date) {

    $week_of_year = sprintf('%02d', date('W', strtotime($date)) + $week_num);
    $day_of_week  = date('N', strtotime($day));
    $timestamp    = strtotime(date('Y') . '-W' . $week_of_year . '-' . $day_of_week);
    return  date('Y-m-d H:i:s', $timestamp);
}

This is working for perfectly for current month, but for the next it gives me plus 1 week date! Please Help!

You leave a lot out of your question and did not answer the questions in my comment, so I have taken a naive approach to answering this question and assumed that the first day of week one is the 1st of the month, regardless of what day it falls on.

I have used PHP's DateTime classes for this. You should read that part of the manual thoroughly and learn how to use these extremely useful classes.

The output you require is not clear from the question, so the function I have created returns an instance of \\DateTime so that you can then format it as you wish.

/** 
 * Returns the date of the given day in a given month.
 * 
 * @param String $month The month in question eg January, September
 * @param Ind $week_num The number of the week, remembering that some months will have a 5th week
 * @param String $day The day to find eg 'Monday'
 * 
 * @return \DateTime
 */
function required_date($month, $week_num, $day)
{
    /**
     * @var \DateTime[] $weeks
     */
    $weeks = array();
    $firstDayOfMonth = new \DateTime("1st $month");
    $lastDayOfMonth = new \DateTime($firstDayOfMonth->format('t M Y'));
    $oneDay = new DateInterval('P1D');
    $period = new \DatePeriod($firstDayOfMonth, $oneDay, $lastDayOfMonth->add($oneDay));

    foreach($period as $date)
    {
        /** @var \DateTime $date */
        $weekNumber = ceil((int)$date->format('d')/7);
        $weeks[$weekNumber][$date->format('l')] = $date;
    }
    return $weeks[$week_num][$day];
}

echo required_date('September', 1, 'Tuesday')->format('Y m d');

Output:

2013 09 03

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