代码之家  ›  专栏  ›  技术社区  ›  stormdrain

在PHP中基于当前日期创建重复日期,并使用Boolean for Done或Not

  •  1
  • stormdrain  · 技术社区  · 15 年前

    埃洛

    我在PHP/MySQL中遇到了一些约会问题。我有一个表单,它提供每年、每季度、每月、每两周或每周的付款周期,我需要能够跟踪付款的时间。

    例如,如果在3月8日星期一(这是输入的日期)、每周四(用户只会看到一个下拉列表提供一周中的几天,因为重复是每周),它会将52个(每周)到期日输入数据库(Julian/任何最简单的日期),并将其中的前9个标记为已支付(自tho以来已过三十天);其余已输入,但标记为未付。

    我有一个表,其中包含每个到期日条目,以及一个到付款详细信息的链接,以及一个“已付款/未付款”布尔字段。

    所以在上面的例子中,付款表看起来像:

    id | pmnt_id | due_date | is_paid
    1  |  0023   | 01/07/10 | 1
    2  |  0023   | 01/14/10 | 1
    3  |  0023   | 01/21/10 | 1
    10 |  0023   | 03/25/10 | 0
    11 |  0023   | 04/01/10 | 0
    

    等。。。

    问题是,好吧,一切。日历系统是如此有效,似乎没有真正的逻辑/直接的方法来做到这一点。我整个上午都在绞尽脑汁,搜索护目镜和PHP日期手册,我只是越来越困惑。

    任何想法/建议/建议都将受到高度赞赏。

    干杯。


    [剪辑]

    $startDate = strtotime("$currentDay $currentMonthFull $currentYear");
    $stTimeFormatted = strftime('%d/%m/%Y',$startDate);
    $numPmnts = ($totalWeeks-$currentWeek);
    for($i=0;$i<$numPmnts;$i++){
        $ddates=array(
        'sched_pay_id'=>$pmntID,
        'due_date'=>date('m/d/y', strtotime('+'.$i.' week', $startDate)),
        'is_paid'=>0,
        );
        $this->db->insert('sched_payments',$ddates);
    }
    
    3 回复  |  直到 13 年前
        1
  •  2
  •   SeanJA    15 年前

    你可能想看看strtotime: http://php.net/manual/en/function.strtotime.php 你可以循环这个过程,直到你达到周期的末尾(在本例中是52周)。

    for i = 1, i <= 52, i++
        insert date(mm / dd / YY, strtotime(+ i week));
    /for
    
        2
  •  0
  •   Anurag    15 年前

    新的php date函数有一个 add 可用于生成所有这些日期的方法。

    $startDate = new DateTime();
    
    // "Period of 1 Week
    $interval = new DateInterval("P1W");
    
    // Will cycle from 1st week's due date to the end of payment cycle
    for($i = 1; $i <= 52; $i++) {
        $dueDate = $date->add($interval);
        $date = $dueDate;
    }
    
        3
  •  0
  •   sra Jon    13 年前

    这是重复天数的一个例子。

    此函数在一段时间内每周返回一个重复日期。 示例:所有星期四从2010年5月6日开始,为期52周。

    <?php 
    //function
    function nextWeeksDay($date_begin,$nbrweek)
    {
    $nextweek=array();
    for($i = 1; $i <= $nbrweek; $i++)  { // 52 week in one year of course
    $nextweek[$i]=date('D d M Y', strtotime('+'.$i.' week',$date_begin));
    }
    return $nextweek;
    }
    /// end function 
    /// example of a select date 
    // var
    $date_begin = strtotime('06-05-2010'); //D Day Month Year  - like function format.
    $nbrweek=52;
    // call function
    $result=nextWeeksDay($date_begin,$nbrweek);
    // Preview 
    for($i = 1; $i <= $nbrweek; $i++)  {
    echo '<br> - '.$result[$i];
    }
    ?>