检查重复日期

发布于 2024-09-05 22:18:37 字数 191 浏览 8 评论 0原文

我正在 PHP 中创建一个日历,数据库中有一个日期,假设是 2010 年 10 月 6 日。

有时我有重复的事件,存储为每周重复,所以如果日期是 6-10-10,并且我从那天(包括那天)起每两周重复一次事件,那么查找每两个事件的日期的最佳方法是什么6-10-10 周?

例如,假设日期是 7-8-10,我如何检查该日期是否符合我的条件?

I am creating a calendar in PHP and in the database have a date, let's say 6-10-10.

Sometimes I have repeating events, stored as weekly repeating, so if the date is 6-10-10, and I am repeating an event every two weeks from that day (including that day) what is the best way to find dates for every two weeks from 6-10-10?

For example, let's say the date is 7-8-10, how can I check to see that date meets my criteria?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2024-09-12 22:18:37

循环是怎么回事?!离散数学 101:如何判断一个数是否为偶数? n%2==0

你如何判断一个日期是否是(呃...)两周一次? date%2weeks==0

抛入偏移量,您将得到 (date-startdate)%2weeks

$startdate = strtotime("10 June 2010");
$otherdate = strtotime("8 July 2010");
$twoweeks  = strtotime("+2 weeks") - time();
if($otherdate>$startdate && (($otherdate-$startdate)%$twoweeks)==0) {
    // this is n * two weeks after
}

What's up with the looping?! Discrete Math 101: How do you figure out if a number is even? n%2==0

How do you figure out if a date is (uh...) biweekly? date%2weeks==0

Toss in an offset and you get (date-startdate)%2weeks

$startdate = strtotime("10 June 2010");
$otherdate = strtotime("8 July 2010");
$twoweeks  = strtotime("+2 weeks") - time();
if($otherdate>$startdate && (($otherdate-$startdate)%$twoweeks)==0) {
    // this is n * two weeks after
}
月依秋水 2024-09-12 22:18:37

您可以使用 DATEDIFF功能。

例如

SELECT * FROM myTable WHERE ( DATEDIFF( myDate,  '2007-12-30' ) % 14 ) = 0

You can use DATEDIFF function.

For example

SELECT * FROM myTable WHERE ( DATEDIFF( myDate,  '2007-12-30' ) % 14 ) = 0
金兰素衣 2024-09-12 22:18:37

如果这是一个日历,那么使用 strtotime 构建有效日期链怎么样?

// 10 dates every two weeks starting next thurdsay

$valid_dates = array();
$date_counter = 0;  
$date_counter = strtotime("next thursday"); // I love being a lazy bastard!

while ($i < 10)
 {
   array_push($valid_dates, $date_counter);
   $date_counter = strtotime("+2 weeks", $date_counter); 
   $i++;

 }

foreach ($valid_dates as $date)
 echo date("Y/m/d", $date)."<br>";

将输出:

2010/06/17
2010/07/01
2010/07/15
2010/07/29
2010/08/12
2010/08/26
2010/09/09
2010/09/23
2010/10/07
2010/10/21

If this is for a calendar, how about building a chain of valid dates using strtotime?

// 10 dates every two weeks starting next thurdsay

$valid_dates = array();
$date_counter = 0;  
$date_counter = strtotime("next thursday"); // I love being a lazy bastard!

while ($i < 10)
 {
   array_push($valid_dates, $date_counter);
   $date_counter = strtotime("+2 weeks", $date_counter); 
   $i++;

 }

foreach ($valid_dates as $date)
 echo date("Y/m/d", $date)."<br>";

Will output:

2010/06/17
2010/07/01
2010/07/15
2010/07/29
2010/08/12
2010/08/26
2010/09/09
2010/09/23
2010/10/07
2010/10/21
初相遇 2024-09-12 22:18:37

Shouldn't result of the DATEDIFF function in MySQL be a multiple of 7 (for events repeating each week), or 14 (for events repeating every two weeks) etc?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文