PHP创建周期性日期和布尔作出或不

问题描述:

ELLO,PHP创建周期性日期和布尔作出或不

我与这里的PHP/MySQL的一些麻烦日期挣扎。我有一份表格,每年,每季度,每月,每两周或每周提供一次付款的重复,并且我需要能够跟踪付款时间。因此,例如,如果在3月8日星期一(即输入日期)添加了某些内容,则每周四会重复出现每周(用户只会看到一个下拉提供一周中的几天 - 因为重复是每周一次),它将进入数据库(朱利安/任何最简单)的52(每周)截止日期,并标记其中前9个支付(因为那些日子已过去);其他人进入,但标为无偿。

我有一个表,每个due_date条目,带有付款明细的链接以及支付/不支付的布尔字段。

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

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); 
} 
+0

如果你的数据库类允许它,你可以通过生成查询字符串做这一切作为一个查询,然后$这个 - > DB->查询(破灭(“;”,$查询));节省您不得不使52次访问数据库,并返回 – SeanJA 2010-02-11 23:26:00

你可能想看看的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 

新的PHP日期函数有一个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; 
} 
+1

谢谢。然而,我使用的是Codeigniter,它似乎不想与'dateTime()' – stormdrain 2010-02-09 19:37:23

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

该函数在一段时间内每周返回一个重现天数。 例: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]; 
} 
?> 
+0

使用''标签打好,否则代码要显示 – sra 2012-01-02 10:22:15