我正在努力了解在给定生日之前他们的年龄是多少以及到另一个生日需要多长时间,但是找不到如何在这些日子之间获得这些日子的方法。
有我的代码
<form action="Vek.php" method="GET">
<input type="text" name="prom1" />
<input type="text" name="prom2" />
<input type="text" name="prom3" />
<input type="submit" />
</form>
<?php
if (isset($_GET["prom1"])) {
$prom1 = $_GET["prom1"];
}else{
$prom1 = '1';
}
if (isset($_GET["prom2"])) {
$prom2 = $_GET["prom2"];
}else{
$prom2 = '1';
}
if (isset($_GET["prom3"])) {
$prom3 = $_GET["prom3"];
}else{
$prom3 = '2000';
}
echo "Zadal jste datum: ".$prom1.".".$prom2.".".$prom3;
echo "<br>";
$datum = Date("j/m/Y", Time()); //today's date
echo "<br>";
echo('Dnešní datum: '.$datum);
echo "<br>";
$test = mktime(0,0,0,$prom2, $prom1, $prom3);
$narozeniny = date("j/m/Y", $test); //day of birthday
echo "<br>";
echo ('Narozeniny: '.$narozeniny);
function zjistiVek($prom1,$prom2,$prom3) //function to find how old are you
{
$vek = time() - mktime(0, 0, 0, $prom2, $prom1, $prom3);
$vek = $vek / (60 * 60 * 24 * 365);
$vek = floor($vek);
return $vek;
}
echo "<br>";
echo('Momentálně je Vám: '.zjistiVek($prom1,$prom2, $prom3).' let');
答案 0 :(得分:2)
我建议在这里使用DateTime
类来轻松获取差异并根据需要对其进行格式化。例如:
$dt1 = new DateTime('2015-01-01'); // an arbitrary time
$dt2 = new DateTime(); // right now's time
$diff = $dt1->diff($dt2); // a DateInterval object
echo $diff->format("%R%a"); // print the difference, in days