通过那里没有任何东西是有价值的(生日)

时间:2017-03-17 02:54:29

标签: php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $email          = $_POST['email'];
        $viewemail      = $_POST['viewemail'];
        $gender         = $_POST['gender'];
        $birthdayyear   = 0 + $_POST['birthdayyear'];
        $birthdaymonth  = 0 + $_POST['birthdaymonth'];
        $birthdayday    = 0 + $_POST['birthdayday'];
        $country        = 0 + $_POST['country'];
        $lang       = 0 + $_POST['lang'];
        $favpet     = $_POST['favpet'];

        if (strlen($birthdayyear) == 2)  {
        $fourdig = date_create_from_format('y', $birthdayyear);
        $birthdayyear = date_format($fourdig, 'Y');
        if ($birthdayyear > date('Y'))
        $birthdayyear = $birthdayyear - 100;
        }


    die($birthdayyear);

页面不会显示值,但如果我写死('sd'。$ birthdayyear);我会得到“sd1988”。这是当我发布值“1988”但是由于某种原因,如果我只发布“88”它就有效..所以我做错了什么?

由于

4 个答案:

答案 0 :(得分:0)

你应该检查变量的类型。

答案 1 :(得分:0)

您正尝试从日期对象中减去100。那样不行。你必须从整数中减去。同样以这种方式比较日期,两个变量都必须是日期对象。所以我也补充说。经过全面测试和工作!

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $email          = $_POST['email'];
    $viewemail      = $_POST['viewemail'];
    $gender         = $_POST['gender'];
    $birthdayyear   = 0 + (int)$_POST['birthdayyear'];
    $birthdaymonth  = 0 + (int)$_POST['birthdaymonth'];
    $birthdayday    = 0 + (int)$_POST['birthdayday'];
    $country        = 0 + (int)$_POST['country'];
    $lang           = 0 + (int)$_POST['lang'];
    $favpet         = $_POST['favpet'];

    if (strlen($birthdayyear) == 2)  {
        $birthdayyear = DateTime::createFromFormat('y', $birthdayyear);
        $birthdayyear->format('Y');
    }
    else {
        $birthdayyear = DateTime::createFromFormat('Y', $birthdayyear);
    }

    if ($birthdayyear > date('Y')) {
        $birthdayyear = (int)$birthdayyear->format('Y');
        $birthdayyear = $birthdayyear - 100;
    }

    echo $birthdayyear;
    exit;

}

答案 2 :(得分:0)

沿着这些方向的东西可以得到你想要的东西。具有2位数年份的date_create_from_format根据docs

假定日期将在1970-2069时间窗口内
<?php

$day = 10;
$month = 12;
$year = 88;

$format = strlen($year) == 2 ? "y-m-d" : "Y-m-d";

$dt = date_create_from_format($format, $year.'-'.$month.'-'.$day)->format("Y-m-d");

echo $dt;

查看here

答案 3 :(得分:0)

添加&#34; 0 + (int) $ _ POST []&#34;对此是正确的解决方案。谢谢谢恩亨利。无论如何,如果有人想要它,那就是工作代码

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$birthdayyear   = 0 + (int)$_POST['birthdayyear'];
$birthdaymonth  = 0 + (int)$_POST['birthdaymonth'];
$birthdayday    = 0 + (int)$_POST['birthdayday'];

    if (strlen($birthdayyear) == 2)  { // if the year value is "88", it will convert it to a four digit number.
        $birthdayyear = date_format(date_create_from_format('y', $birthdayyear), 'Y');
    if ($birthdayyear > date('Y')) // if the year is over the present year
        $birthdayyear = $birthdayyear - 100; // 2035 will become 1935
    }

$birthday = date("Ymd", strtotime($birthdayyear.'-'.$birthdaymonth.'-'.$birthdayday)); // returns "YYYYmmdd"
echo $birthday;
}

感谢所有帮助人员!