PHP-检查用户是否';输入的日期是未来日期还是今天日期

PHP-检查用户是否';输入的日期是未来日期还是今天日期,php,validation,date,Php,Validation,Date,我有一个文本框来输入日期 检查用户输入的日期是将来的还是今天的代码应该是什么样子 我还希望用户以dd/mm/yyyy格式输入日期 如果其中任何一个失败,代码将给出一个错误。使用 int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("

我有一个文本框来输入日期

检查用户输入的日期是将来的还是今天的代码应该是什么样子

我还希望用户以dd/mm/yyyy格式输入日期

如果其中任何一个失败,代码将给出一个错误。

使用

int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

因此,填写参数并减去

time()
如果大于零,则在将来使用

int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

因此,填写参数并减去

time()
如果大于零,则在将来

您也可以使用该类进行此比较:

$now       = new DateTime();
$user_date = DateTime::createFromFormat('d/m/Y', $userDate);
if ($user_date >= $now)
{
     echo 'Date is not in the past';   
}
您也可以使用该类进行此比较:

$now       = new DateTime();
$user_date = DateTime::createFromFormat('d/m/Y', $userDate);
if ($user_date >= $now)
{
     echo 'Date is not in the past';   
}

我推荐这种解决方案:

/**
 * Is future date
 *
 * Checks if given date is today
 * or in the future.
 *
 * @param string $date
 *
 *
 * @return bool
 *
 */
protected function isFutureDate($date)
{
    // assuming the date is in this format
    // (I use value objects for these things so that
    // I don't have to repeat myself)

    $date = DateTimeImmutable::createFromFormat('d-m-Y', $date);
    $today = DateTimeImmutable::createFromMutable(new DateTime());
    return $date->getTimestamp() >= $today->getTimestamp();
}
我使用以下Date对象,该对象由任何其他Date类扩展。其他日期类将有自己的规则(如日期必须在未来等),这些规则从该基类扩展而来:

/**
 * Class DateValueObject
 *
 * 
 *
 * @package Hidden\Model\Shared\ValueObject
 *
 */
abstract class DateValueObject extends ValueObject
    implements DateValueObjectContract
{

    /**
     * @var DateValueObject
     *
     */
    protected $value;


    /**
     * Initialises the Date.
     *
     * @param $date
     *
     * @throws InvalidDateException
     *
     */
    public function __construct($date)
    {
        if (is_string($date)) {
            $this->value = $this->setDate($date);
        } else {
            throw new InvalidDateException(
                'String Expected. Got: ' .$date
            );
        }
    }


    /**
     * Set valid date.
     *
     * @param string $date
     *
     * @return DateTimeImmutable
     *
     * @throws InvalidDateException
     */
    protected function setDate($date)
    {
        try {
            $d = DateTimeImmutable::createFromFormat('d-m-Y', $date);
            if($d && $d->format('d-m-Y') == $date) {
                return $d;
            } else {
                $d = DateTimeImmutable::createFromFormat('d/m/Y', $date);
                if($d && $d->format('d/m/Y') == $date) {
                    return $d;
                }
                throw new InvalidDateException(
                    'Date cannot be formatted: ' .$date
                );
            }
        } catch (\Exception $e) {
            throw new InvalidDateException(
                'For date: ' .$date .' with message' .$e->getMessage()
            );
        }
    }



    /**
     * Get the date to string
     *
     * @param string $format
     *
     * @return string
     *
     */
    public function toString($format = 'd/m/Y')
    {
        return $this->value->format($format);
    }


    /**
     * Get the date as immutable object.
     *
     *
     * @return DateTimeImmutable|Date
     *
     */
    public function toDateObject()
    {
        return $this->value;
    }
}
编辑:请注意,第一个代码块检查日期是否在未来,而第二个示例用于具有可扩展类,以便其他日期类(生日、InvoiceDate等)可以从中扩展,而无需重复代码。您可以将其放在一个方法中,并在每次需要时保持复制和粘贴,或者只是从中扩展,知道它可以全面工作


我的示例接受“d-m-Y”和“d/m/Y”,并相应地处理格式。最后,您仍然有一个php DateTimeImmutable对象,但它知道如何构建自身。

我推荐此解决方案:

/**
 * Is future date
 *
 * Checks if given date is today
 * or in the future.
 *
 * @param string $date
 *
 *
 * @return bool
 *
 */
protected function isFutureDate($date)
{
    // assuming the date is in this format
    // (I use value objects for these things so that
    // I don't have to repeat myself)

    $date = DateTimeImmutable::createFromFormat('d-m-Y', $date);
    $today = DateTimeImmutable::createFromMutable(new DateTime());
    return $date->getTimestamp() >= $today->getTimestamp();
}
我使用以下Date对象,该对象由任何其他Date类扩展。其他日期类将有自己的规则(如日期必须在未来等),这些规则从该基类扩展而来:

/**
 * Class DateValueObject
 *
 * 
 *
 * @package Hidden\Model\Shared\ValueObject
 *
 */
abstract class DateValueObject extends ValueObject
    implements DateValueObjectContract
{

    /**
     * @var DateValueObject
     *
     */
    protected $value;


    /**
     * Initialises the Date.
     *
     * @param $date
     *
     * @throws InvalidDateException
     *
     */
    public function __construct($date)
    {
        if (is_string($date)) {
            $this->value = $this->setDate($date);
        } else {
            throw new InvalidDateException(
                'String Expected. Got: ' .$date
            );
        }
    }


    /**
     * Set valid date.
     *
     * @param string $date
     *
     * @return DateTimeImmutable
     *
     * @throws InvalidDateException
     */
    protected function setDate($date)
    {
        try {
            $d = DateTimeImmutable::createFromFormat('d-m-Y', $date);
            if($d && $d->format('d-m-Y') == $date) {
                return $d;
            } else {
                $d = DateTimeImmutable::createFromFormat('d/m/Y', $date);
                if($d && $d->format('d/m/Y') == $date) {
                    return $d;
                }
                throw new InvalidDateException(
                    'Date cannot be formatted: ' .$date
                );
            }
        } catch (\Exception $e) {
            throw new InvalidDateException(
                'For date: ' .$date .' with message' .$e->getMessage()
            );
        }
    }



    /**
     * Get the date to string
     *
     * @param string $format
     *
     * @return string
     *
     */
    public function toString($format = 'd/m/Y')
    {
        return $this->value->format($format);
    }


    /**
     * Get the date as immutable object.
     *
     *
     * @return DateTimeImmutable|Date
     *
     */
    public function toDateObject()
    {
        return $this->value;
    }
}
编辑:请注意,第一个代码块检查日期是否在未来,而第二个示例用于具有可扩展类,以便其他日期类(生日、InvoiceDate等)可以从中扩展,而无需重复代码。您可以将其放在一个方法中,并在每次需要时保持复制和粘贴,或者只是从中扩展,知道它可以全面工作


我的示例接受“d-m-Y”和“d/m/Y”,并相应地处理格式。最后,您仍然有一个php DateTimeImmutable对象,但它知道如何构建自己。

应该使用时间戳比较日期。请查看我的答案以获取示例。@Keith为什么日期只能作为时间戳进行比较?DateTime对象具有可比性,可读性更高。我将在回答中更好地解释。但是还要注意DateTimeImmutable::createFromFormat需要作为第二个参数传递时间字符串,并且您没有检查格式,因此以下操作可能会出错,代码不会抛出任何内容:用户日期:2015年11月10日。如果有人通过了2015年10月11日而不是2015年11月10日,结果将是错误的,没有人会注意到。应使用时间戳比较日期。请查看我的答案以获取示例。@Keith为什么日期只能作为时间戳进行比较?DateTime对象具有可比性,可读性更高。我将在回答中更好地解释。但是还要注意DateTimeImmutable::createFromFormat需要作为第二个参数传递时间字符串,并且您没有检查格式,因此以下操作可能会出错,代码不会抛出任何内容:用户日期:2015年11月10日。如果有人通过了2015年10月11日而不是2015年11月10日,那么结果将是错误的,没有人会注意到。您断言应该比较日期,因为时间戳是错误的。你写了很多代码来做DateTime已经做过的事情,并且做得更好;时间戳不具有时区意识或夏令时意识。您认为应该比较日期,因为时间戳是不正确的。你写了很多代码来做DateTime已经做过的事情,并且做得更好;时间戳不支持时区或夏令时