【发布时间】:2022-01-26 15:41:14
【问题描述】:
我在 php 中有一个代码,用于处理一个人的个人详细信息。我需要代码来处理日期输入,这样当用户输入像 1900 这样的年份时,程序会输出 这是不可能的。如果日期输入在未来,我已成功处理程序应如何响应。 该程序以 欧洲日期格式 获取用户输入的日期,例如 21-10-1990, Am很难处理这个问题,因为 php 内置函数 time() 返回自 1970 年 1 月 1 日以来测量的 unix 时间戳/em>。有没有办法可以绕过这一点来实现从 1900 年及以下开始的年份的检测,而无需直接将条件结构应用于年份?
代码
<?php
class User{
//initialize the user properties
public string $name="";
public string $dob="";
public string $national_id="";
public string $tel="";
public string $email="";
public function User($user_name,$dob,$national_id,$tel,$mail){
//assign the values inside the constructor
$this->name=$user_name;
$this->dob=$dob;
$this->national_id=$national_id;
$this->tel=$tel;
$this->email=$mail;
}
function validate_dob(){
//get a new time in millis from the date passed in
$date_obj=strtotime($this->dob);
//add conditional structures to make sure the date is not in the
//future and is within acceptable time range
//get the current time from the unix timestamp
$current_time=time();
if($date_obj>$current_time){
echo "\n";
echo "That is impossible, you birth date cannot be in the future";
}
//if the date is also 1900 and below then also throw an error, i need help here
}
}
【问题讨论】:
-
我强烈推荐使用 PHP 的
DateTime类 php.net/manual/en/class.datetime.php,而不是time()和strtotime() -
它适用于处理未来的出生日期,我只是想让它适用于疯狂的日期时间,如 1900、1899 等等,我愿意通过上述课程的答案提出建议
-
使用IntlCalendar。
-
如果您使用
>处理未来的日期,您可以使用<处理过去的日期。 UTC 时间 1900-01-01 午夜的 Unix 时间是-2208988800。我对这里提出的确切问题有点困惑,因为你提到了很多不同的事情。