[英]How to calculate the age of a user's input in years and months using php?
我被指派建立销售点系统。 客户将根据其年龄进行收费,如果用户年龄超过15.5岁,则需要为产品收取$ 50的费用。 如果用户年龄不足15.5岁,则需要支付25美元。
这是我到目前为止的代码:
<html>
<head>
<title>Age Test</title>
</head>
<body>
<form action="age_test.php" method="post">
<input type="text" name="first" placeholder="Enter first name">
<br>
<input type="text" name="last" placeholder="Enter last name">
<br>
<input type="date" name="date">
<br>
<label for="rush"> Check for a rush order(A $200 fee will apply.)</label>
<input type="checkbox" name="rush">
<br>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
// echo "submit is set";
$date = $_POST['date'];
$age = date("Y/m/d") - $date;
echo $age;
//Users age will determine the price.
if($age >= 15.5){
echo "<br>Your price is $50";
}elseif ($age < 15.5) {
echo "<br>Your price is $25";
}
if(isset($_POST['rush'])){
// echo "<br>$200 is applied to payment";
}
}
?>
</body>
</html>
我所拥有的解决方案因此没有给我十进制数。 我想要一些有关如何产生小数的帮助。 任何帮助将不胜感激。
我认为您在这种情况下正在寻找number_format
函数。
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
以下是与您的作业相关的示例:
number_format((float)$age, 2, '.', '');
查看文档 。
这是一种计算方法,同时考虑了时区。 我已经更新了答案,而是用几个月来计算。
$dateInput = $_POST['date'];
$timeZone = new DateTimeZone('America/New_York');
$bday = DateTime::createFromFormat('Y/m/d', $dateInput , $timeZone);
$diff = $bday->diff(new DateTime());
$months = $diff->format('%m') + 12 * $diff->format('%y');
//returned number of months and divide by 12.
//12 months make a year.
$age = $months/ 12;
echo $age;
默认情况下,这将返回一个浮点数。
//并且您始终可以舍入到小数点后两位
echo number_format((float)$age, 2, '.', '');
希望这可以帮助
尝试简单的方法:)
<?php
$input = '1970/01/01'; // $_POST['date']
$now = new \DateTime();
$diff = $now->diff(new \DateTime($input));
$price = ($diff->y > 15 && $diff->m > 6) ?50 : 25;
echo $price;
<?php
// your date you will display from database
$date = "2012-05-06";
$date = new DateTime($date);
/*
* for the current year,month and day you will
* date('y') --> to print current day
* date('m') --> to print the current month
* date('y') --> to print the current day
*/
$y = date('Y');
$m = date('m');
$d = date('d');
$full_date ='P'.$y.'Y'.$m.'M'.$d.'D';
/*
for example
* we will subtract (2012-05-06) - (2017-07-26)
*
* -0006 for year and 02 month and 20 day
*
*/
$date ->sub(new DateInterval(".$full_date."));
/*
* you should ltrim (-) from years
*/
$str = ltrim($date ->format('y'),'-');
/*
* you should ltrim (0) from month
*/
$str2 = ltrim($date ->format('m'),'0');
//output 1
echo $str.' years and '.$str2 .' months';
// output 2
echo $str.'.'.$str2;
?>
输出将是
6 years and 9 months
6.9
有关减去日期和时间的更多信息,请检查此链接减去日期和时间
您的计算问题
$age = date("Y/m/d") - $_POST['date'];
是-不管客户端使用哪种日期格式-您实际上都在试图将两个string
值相减,从而导致将它们隐式转换为int
。
也就是说,只要两个string
先从这一年中, 出现计算工作; 除了它永远不会包含您要查找的分数。
有关示例,请参见https://3v4l.org/qskMD 。
为了解决您的问题,请先尝试计算以年为单位的差额,然后找到剩余的天数,然后除以一年中的天数:
// create a `DateTimeImmutable` object from the date provided (adjust format as needed)
$birthday = \DateTimeImmutable::createFromFormat(
'Y-m-d',
$_POST['date']
);
// current date and time
$now = new \DateTimeImmutable();
// find the total difference
$totalDifference = $now->diff($date);
// get difference in total years only
$years = (int) $difference->format('y');
// create a `DateTimeImmutable` object, going back full years from now only
$yearsAgo = $now->diff(new \DateInterval(sprintf(
'P%sY',
$years
));
// get difference between birthday and going back full years
$remainingDifference = $yearsAgo->diff($birthday);
// get difference in months
$months = $remainingDifference->format('m');
$age = $years + $months / 12;
供参考,请参阅:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.