简体   繁体   English

通过使用循环传递月份和星期的星期数来获取日期?

[英]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! 这对于本月来说是完美的,但是对于下个月,它给了我加上1周的日期! 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. 我为此使用了PHP的DateTime类。 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. 您需要的输出在问题中并不清楚,因此我创建的函数将返回\\DateTime的实例,以便您可以根据需要对其进行格式化。

/** 
 * 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 2013 09 03

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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