【问题标题】:(PHP) Check for valid day and month combination(PHP) 检查有效的日期和月份组合
【发布时间】:2016-05-18 22:58:26
【问题描述】:

晚上!

我正在尝试学习 PHP 中的 OO 编程,我想检查给定日期在给定月份中是否有效。例如:01-31-2016 有效(因为一月有 31 天),04-31-2016 无效(因为四月只有 30 天)。我认为这可以通过 checkdate() 完成,但我正在努力完成这项工作。

这是我目前得到的:

<?php
class birthDate {
 public $birthday;
 public $birthmonth;
 public $birthyear;

 public function __construct($birthday, $birthmonth, $birthyear) {
    $this->birthday = $birthday;
    $this->birthmonth = $birthmonth;
    $this->birthyear = $birthyear;
}

 public function setBirthdate($birthday, $birthmonth, $birthyear) {
    if (checkdate($birthmonth, $birthday, $birthyear) == TRUE) {
        $this->birthday = $birthday;
        $this->birthmonth = $birthmonth;
        $this->birthyear = $birthyear;
    } else {
        $birthday = 0;
        $birthmonth = 0;
        $birthyear = 0;
  }
}

 public function getBirthdate() {
    if ($this->birthday == 0 && $this->birthmonth == 0 && $this->birthyear == 0) {
        $temp = "Not possible";
  } else {
        $temp  = $this->birthday   . "-";
        $temp .= $this->birthmonth . "-";
        $temp .= $this->birthyear;  
  }
return $temp;
}

 public function printBday() {
    echo "<strong>Birthday: \t</strong>" . $this->getBirthdate();
  }
}

$date = new birthDate(4, 31, 1991);
$date->printBday();
?>

我认为我没有以正确的方式使用 checkdate 函数,但我无法弄清楚。如果日期有效,则应打印日期。如果日期无效,则应打印$temp。然而,目前每个日期都会被打印出来,无论它是有效的还是无效的。我做错了什么?

【问题讨论】:

  • 你有错误吗?如果你得到,请向你的问题添加错误报告

标签: php


【解决方案1】:

看起来唯一的问题是,您没有在构造函数中调用setBirthdate()

编辑:就像 TheDrot 说的,这应该可行:

<?php
class birthDate {
 public $birthday;
 public $birthmonth;
 public $birthyear;

 public function __construct($birthday, $birthmonth, $birthyear) {
    $this->setBirthdate($birthday, $birthmonth, $birthyear);
}

 public function setBirthdate($birthday, $birthmonth, $birthyear) {
    if (checkdate($birthmonth, $birthday, $birthyear) == TRUE) {
        $this->birthday = $birthday;
        $this->birthmonth = $birthmonth;
        $this->birthyear = $birthyear;
    } else {
        $this->birthday = 0;
        $this->birthmonth = 0;
        $this->birthyear = 0;
  }
}

 public function getBirthdate() {
    if ($this->birthday == 0 && $this->birthmonth == 0 && $this->birthyear == 0) {
        $temp = "Not possible";
  } else {
        $temp  = $this->birthday   . "-";
        $temp .= $this->birthmonth . "-";
        $temp .= $this->birthyear;  
  }
return $temp;
}

 public function printBday() {
    echo "<strong>Birthday: \t</strong>" . $this->getBirthdate();
  }
}

$date = new birthDate(4, 31, 1991);
$date->printBday();
?>

【讨论】:

  • 同样在这个函数的 else 块中,设置为 0 的变量是本地而不是类属性,因为缺少 $this-&gt;
  • @Matthijs2504,只是为了在这个答案中添加一件事,因为您正在通过 settergetter 方法访问所有实例属性,最好将所有属性设置为private
【解决方案2】:

或者你可以像这样使用checkdate

bool checkdate ( int $month , int $day , int $year )

参数

月份:月份介于 1 和 12 之间。

:天在给定月份的允许天数内。 闰年被考虑在内。

年份:年份介于 1 到 32767 之间。

返回值

如果给定的日期是有效,则返回 TRUE;否则返回 FALSE

$info1 = checkdate(4, 30, 1991);
var_dump($info1);

bool(true) 如果日期有效

$info2 = checkdate(4, 31, 1991);
var_dump($info2);

bool(false) 如果日期无效

【讨论】:

    猜你喜欢
    • 2016-11-28
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多