日期与php的差异 [英] Dates difference with php

查看:100
本文介绍了日期与php的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有人可以帮助我吗?



我有两个日期在两个不同的领域输入startDate和endDate。



在输入时,我想显示警告:




  • 第二个是在第一个之前的日期。所以这是错误的。

  • ,第一个和第二个之间的一年中的最短间隔至少为3天,另外一年的其他时间为7天



我正在考虑编写一个PHP函数,但是如果输入第二个日期,我该如何调用? >

许多感谢您的帮助
Francesco

解决方案

转换您的日期为朱利安日 gregoriantojd

  / ** 
*获取日期的朱利安日。朱利安日是公元前4713年1月1日以后的天数。
* /
函数datetojd($ date)
{
返回gregoriantojd(idate('m',$ date),
idate('d',$ date ),
idate('Y',$ date));
}

//您可以使用strtotime来解析大量日期格式,假设它们是文本
$ startDate = strtotime('2009年11月22日');
$ finishDate = strtotime('2009年11月26日');

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

if($ diff< 0){
// oops,$ finishDate在$ startDate之前
}
else {
// check $差异至少为3或7,取决于日期
}


Hi guys I was wondering if anyone could help me with the following:

I have two dates entered in two different fields > startDate and endDate.

As they are entered I would like to show a warning if:

  • the second one is a date before the first one. So it is wrong.
  • and that between the first one and the second one there a minimum gap of at least 3 days during certain period of the year and 7 days during other periods of the year.

I was thinking to write a PHP function but how do I call it as soon as the second date is entered?

Many many thank for you help Francesco

解决方案

Convert your dates to Julian day with gregoriantojd.

/**
 * 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
}

这篇关于日期与php的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆