【发布时间】: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