I've got an input field where I type in a deadline in words. For example, "5 minutes", "2 days", "6 weeks", "8 months". What I want for the program to do is to calculate how long it will take when that deadline ends. And also if that deadline is almost ending, for example if 80% of the given time has passed.
我有一个输入字段,我在文字的截止日期输入。例如,“5分钟”,“2天”,“6周”,“8个月”。我想让程序做的是计算截止日期结束所需的时间。并且如果截止日期即将结束,例如,如果已经过了80%的给定时间。
I was thinking something like that php splits the given time in seconds, and then checks how many minutes and hours or days fit in those seconds and then puts that in dateTime. Like current date + input = futureDate.
我在想像php那样以秒为单位分割给定时间,然后检查那些秒内适合的分钟数和小时数或天数,然后将其放入dateTime中。像当前日期+输入= futureDate。
I know I probably shouldn't use percentages, it's just an example.
我知道我可能不应该使用百分比,这只是一个例子。
<input type="text" name="getFutureTime">
<?php
$futureTime = $_POST['getFutureTime'];
$dateNow = date('d-m-Y H:i:s');
if($futureTime > $dateNow){
//Calculate
echo "Deadline has passed";
}else if (($futureTime / 100 * 80) < $dateNow){
//Calculate
echo "Deadline is almost passed";
}
?>
3 个解决方案
#1
Here the values are string
and you cant compare this like that. Convert them to timestamp
first. Try with -
这里的值是字符串,你不能比较这样。首先将它们转换为时间戳。尝试 -
$futureTime = strtotime($_POST['getFutureTime']);
$dateNow = time();
#2
I have a solution with, caculate how date later :)..., copy this to localhost and run :) diff
我有一个解决方案,计算以后的日期:)...,将其复制到localhost并运行:) diff
$d1=new DateTime("now");
$d2=new DateTime($_POST['DateInput']);
$diff = $d1->diff($d2);
echo "<pre>";
print_r($diff);
echo "</pre>";
$date = $diff->format('%d'); //It date
$year = $diff->format('%y'); //It year
$month = $diff->format('%m'); //It month
echo $diff->format('%a'). "<br/>";
foreach($diff as $key => $value){
echo $key . "<br/>";
echo $value;
}
$value = strtotime('14/12/2012');
echo($value);
Update
$diff->format('%a') // Is date from now example 10/12/2015 - 10/5/2015 = 7
$ diff-> format('%a')//从现在开始的例子2015年10月10日 - 2015年10月5日= 7
$diff->format('%a') is 7
$ diff-> format('%a')是7
#3
Try this it will work:
试试这个会起作用:
$date_a = new DateTime('2015-05-07 13:03:48');
$date_b = new DateTime('2015-02-04 13:03:41');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%m Months %d Days %h Hours %i Minutes %s Seconds');
#1
Here the values are string
and you cant compare this like that. Convert them to timestamp
first. Try with -
这里的值是字符串,你不能比较这样。首先将它们转换为时间戳。尝试 -
$futureTime = strtotime($_POST['getFutureTime']);
$dateNow = time();
#2
I have a solution with, caculate how date later :)..., copy this to localhost and run :) diff
我有一个解决方案,计算以后的日期:)...,将其复制到localhost并运行:) diff
$d1=new DateTime("now");
$d2=new DateTime($_POST['DateInput']);
$diff = $d1->diff($d2);
echo "<pre>";
print_r($diff);
echo "</pre>";
$date = $diff->format('%d'); //It date
$year = $diff->format('%y'); //It year
$month = $diff->format('%m'); //It month
echo $diff->format('%a'). "<br/>";
foreach($diff as $key => $value){
echo $key . "<br/>";
echo $value;
}
$value = strtotime('14/12/2012');
echo($value);
Update
$diff->format('%a') // Is date from now example 10/12/2015 - 10/5/2015 = 7
$ diff-> format('%a')//从现在开始的例子2015年10月10日 - 2015年10月5日= 7
$diff->format('%a') is 7
$ diff-> format('%a')是7
#3
Try this it will work:
试试这个会起作用:
$date_a = new DateTime('2015-05-07 13:03:48');
$date_b = new DateTime('2015-02-04 13:03:41');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%m Months %d Days %h Hours %i Minutes %s Seconds');