Php日期时间功能无法识别1000之前的日期

时间:2015-08-24 12:02:27

标签: php

此代码用于获取从表单输入的值,但这不需要一年输入0100作为0100但是作为1915,然后将其与我在其他一个问题中看到的JS一起使用这里的任何帮助都会非常好的,我认为这个问题是需要做的事情,但是我无法让这个工作正常。这是php的限制吗?

<?php
$year = "";
$month = "";
$day = "";

if (isset($_GET['year']) && !empty($_GET['year'])) {
    $year = $_GET['year'];
}

if (isset($_GET['month']) && !empty($_GET['month'])) {
    $month = $_GET['month'];

    $monthNumber = date('m', strtotime("$month 1 Y"));
}

if (isset($_GET['day']) && !empty($_GET['day'])) {
    $day = $_GET['day'];
}

if ($year != "" && $monthNumber != "" && $day != "") {





    $fullUrlDate = $year . "-" . $monthNumber . "-" . $day;

    $urlDate = new DateTime(date($fullUrlDate));
    $today =  new DateTime(date("Y-m-d H:i:s"));

    $interval = $urlDate->diff($today);

    $gapYears = $interval->y;
    $gapMonths = $interval->m;
    $gapDays = $interval->d;
    $gapDaysTotal = $interval->days;
    $gapWeeksTotal = round($interval->days/7);
    $gapHours = $interval->h;
    $gapMinutes = $interval->i;
    $gapSeconds = $interval->s;

    if ($gapWeeksTotal == 1) {
        $gapWeeksSuffix = "";
    } else {
        $gapWeeksSuffix = "s";
    }   

    if ($gapDays == 1) {
        $gapDaysSuffix = "";
    } else {
        $gapDaysSuffix = "s";
    }   

    $ordinalSuffix = date("S", strtotime($fullUrlDate));

    if (strtotime($fullUrlDate) < strtotime(date("Y-m-d H:i:s")) ) {
        $dateInThePast = true;
    } else {
        $dateInThePast = false;
    }

    // Months gap
    $monthsInterval = date_diff($urlDate, $today);
    $monthsGap = $monthsInterval->m + ($monthsInterval->y * 12);    

    $gapMonthsSuffix = ($monthsGap == 1 ? "" : "s");

1 个答案:

答案 0 :(得分:2)

DateTime没有这样的限制,但是用于初始化它的date函数确实如此。您可以使用DateTime::setDate设置任意年份:

php > $a = new DateTime("2015-08-24");
php > echo $a->format(DateTime::ISO8601);
2015-08-24T00:00:00+0000

php > $a->setDate(90, 8, 24);
php > echo $a->format(DateTime::ISO8601);
0090-08-24T00:00:00+0000

php > $a->setDate(90090, 8, 24);
php > echo $a->format(DateTime::ISO8601);
90090-08-24T00:00:00+0000