1

你好,

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

因此,例如,如果在 3 月 8 日星期一(即输入的日期)添加了某些内容,在星期四重复每周一次(用户只会看到提供一周中的几天的下拉菜单 - 因为重复是每周一次),它会将 52 个(每周)到期日输入数据库(朱利安/最简单的),并标记前 9 个已付款(因为那些日子已经过去);其余的进入但标记为未付款。

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

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

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

ETC...

问题是,好吧,一切。日历系统如此完善,似乎没有真正合乎逻辑/直接的方法来做到这一点。我整个早上都在绞尽脑汁,搜索护目镜和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);
}
4

3 回答 3

2

您可能想查看 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
于 2010-02-09T17:37:00.140 回答
0

新的 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;
}
于 2010-02-09T17:57:40.727 回答
0

这是重复天数的示例。

此函数返回一个周期内每周的重复日。示例:从 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];
}
?>
于 2010-05-06T15:30:34.497 回答