0

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.

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.

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";
    }
?>
Vasco
  • 191
  • 1
  • 2
  • 12
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Narendrasingh Sisodia May 07 '15 at 08:43
  • @Uchiha that post converts time into words, I want to do it the other way around; words to date. – Vasco May 07 '15 at 08:48

3 Answers3

0

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();
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • Why should the OP "try this"? Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO. – Rizier123 May 07 '15 at 08:38
0

I have a solution with, caculate how date later :)..., copy this to localhost and run :) 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') is 7

HoangHieu
  • 2,802
  • 3
  • 28
  • 44
  • How would this work with words such as "months" and "weeks"? That's what I'm struggling with. Also, I don't want a fixed date, I want the program to get the date from an input field. – Vasco May 07 '15 at 09:57
0

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');
Touqeer Shafi
  • 5,084
  • 3
  • 28
  • 45