簡體   English   中英

PHP-檢查用戶輸入的日期是將來的日期還是今天的日期

[英]PHP - Check if user's entered date is of future or of today

我有一個文本框輸入日期。

該代碼應如何檢查用戶輸入的日期是將來的日期還是今天的日期?

我還希望用戶以dd / mm / yyyy格式輸入日期。

如果其中任何一個失敗,該代碼將給出錯誤。

您也可以使用DateTime類進行此比較:

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

采用

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()

如果它大於零,那就是將來

我推薦此解決方案:

/**
 * 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對象。 其他日期類將有自己的規則(例如,日期必須在將來等),這些規則是從該基本類擴展而來的:

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

編輯:請注意,第一段代碼檢查日期是否在將來,而第二個示例用於具有可擴展類,以便其他日期類(Birthday,InvoiceDate等)可以從擴展而不必重復代碼。 您可以將其放入方法中,並在需要時保留復制並粘貼它,或者從它進行擴展,直到知道它可以全面使用。

我的示例接受“ dmY”和“ d / m / Y”並相應地處理格式。 在一天結束時,您仍然有一個php DateTimeImmutable對象,但是知道如何構建自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM