2009-11-26 78 views
0

嗨,大家好我想知道如果有人能帮助我的以下内容:日期用PHP差异

我有两个不同的字段中输入两个日期>的startDate和结束日期。

以它们输入我想显示一个警告,如果:

  • 第二个是第一个之前的日期。所以这是错误的。
  • 而第一个和第二个之间的最小差距在一年中的某个时间段至少为3天,而在其他时间段为7天。

我正在考虑编写一个PHP函数,但是如何在第二次输入日期后立即调用它?

许多许多感谢您的帮助 弗朗西斯

+1

不,这是StackOverflow,其中像这样的诚实问题由爱好者回答。 – 2009-11-26 11:14:38

回答

1

将您的日期转换为Julian daygregoriantojd

/** 
* Get the Julian day of a date. The Julian day is the number of days since 
* January 1, 4713 BC. 
*/ 
function datetojd($date) 
{ 
    return gregoriantojd(idate('m', $date), 
         idate('d', $date), 
         idate('Y', $date)); 
} 

// you can use strtotime to parse a lot of date formats, assuming they are text 
$startDate = strtotime('22nd Nov 2009'); 
$finishDate = strtotime('26nd Nov 2009'); 

$diff = datetojd($finishDate) - datetojd($startDate); 

if ($diff < 0) { 
    // oops, $finishDate is before $startDate 
} 
else { 
    // check $diff is at least 3 or 7 depending on the dates 
} 
+0

小记:改为使用格雷戈里的caldar,他也可以在unix时间戳里转换日期 – Strae 2009-11-26 11:55:02

+0

谢谢Alex,我会对你的建议。 – frapet 2009-11-26 16:41:20

1

请在使用JavaScript客户端的检查。

然后执行相同的检查服务器端,可以在表单提交后显示一条消息(对于少数用户使用Javascript运行禁用?)。

0

1°的情况下:

SELECT [whatever you need from the table] WHERE endDate < startDate 

2°的情况下:

SELECT [whatever you need from the table] WHERE (endDate - startDate) >= IF([select that define in wich period of the year the data are],3, 7) 

本病的做的伎俩,但可能你的问题不能得到解决SQL端。

请更好地描述你需要做的事情。

编辑:

好,然后根据别人的建议,首先检查被JS HTEM(为了方便,而不是安全:从未对JS验证只依赖!)

使用strtotime为比较/操作。

EDIT2(最后): 围棋与Alex's Answer

+0

日期尚未输入数据库。他们会,如果他们是正确的。我需要做的唯一事情是在输入数据库之前检查输入的日期是否正确。 – frapet 2009-11-26 11:17:12

0

我不知道你是否能尽快进入第二日称,除非你重新载入页面或对另一功能页面可能会有点复杂

我会检查日期的方式是使用php的mktime函数,它会给你unix时间。那么如果第二个数字小于第一个数字,第二个数字在前,如果第二个数字小于第一个+ 3 * 24 * 60 * 60(3天内的秒数),那么它不相隔3天