0

I only have basic PHP knowledge and I'm using PHP+Mysql and trying to check the difference in days between 2 dates; the 1st date is formatted by myself in the script as a string:

$f_entrega=$_POST['year1']."-".$_POST['month1']."-".$_POST['day1'];

The second date ($f_dock) which is the one causing the issue is taken from the mysql database which column is in DATE format. To compare the dates I do the following:

if(!empty($_POST["id"])){
    $f_entrega=$_POST['f_entrega_a']."-".$_POST['f_entrega_m']."-".$_POST['f_entrega_d'];
    $f_entr=$f_entrega;
    $mysqli=conectar();
    $resultado = $mysqli->query("SELECT id,f_dock FROM pt");
    $row = $resultado->fetch_assoc();
    for ($i=0;$i<count($ids);$i++){
        do{
            if ($ids[$i]==$row["id"]){
                $f_dock=$row["f_dock"];
                break;
            }
        } while ($row = $resultado->fetch_assoc());     
        $error=0;
        var_dump($f_dock);
        $f_dock=strtotime($f_dock);
        $f_dock=date('Ymd',$f_dock);
        $f_entrega=$f_entr;
        $f_entrega=strtotime($f_entrega);
        $f_entrega=date('Ymd',$f_entrega);
        $f_dock=DateTime::createFromFormat('Ymd',$f_dock);
        $f_entrega=DateTime::createFromFormat('Ymd',$f_entrega);
        $dias_vendor=date_diff($f_dock,$f_entrega);
        $tat=$dias_vendor->format('%R%a');

Sometimes it works correctly, but other times I get Warning: strtotime() expects parameter 1 to be string, object given in [first line] and $tat is not correctly calculated and has strange values. I've tried different solutions like $f_dock=(string)$f_dock before but finally the convertion always fails in some cases. Thanks in advance for any tip.

2
  • This error Warning: strtotime()expects parameter 1 to be string said that $f_entrega is empty or non valid string to format dates. You can control it viewing the value in a var_dump: var_dump($f_entrega) before the strtotime function. Tell us what is the result of this var_dump when script fails Commented Aug 13, 2015 at 8:58
  • The output for var_dump is the following: string(10) "2015-08-11" Commented Aug 13, 2015 at 9:26

1 Answer 1

1

The error that you are getting is because the string you are entering is not a valid string for the strtotime() function to convert.

For instance 2015-08-31 will convert just fine, as will today, tomorrow or +7 days.

For more specific help you will need to tell us what the value of $f_dock is (as Marcos says in his comment, var_dump($f_dock) will get you this).

However, on to the solution:

$date1 = strtotime($f_dock); //timestamp in seconds
$date2 = strtotime($f_entrega); //same for the second date
$difference = $date1 - $date2; //difference in seconds between the dates
$days = floor($difference/86400); 

86400 is the number of seconds in a day, so find out how many seconds difference there is, then see how many days worth of seconds are in there and use floor() to round the number down. Job done.

Sign up to request clarification or add additional context in comments.

9 Comments

The output for var_dump is the following: string(10) "2015-08-11"
That works for me and should for you, even if there is a space after the 11. Is that code on your original post an exact copy and paste?
Yes it is...could be something wrong in the database that sometimes the f_dock is a string and others an object? The mysql database column is set as DATE, and the straction is always the same by "row=$result->fetch_asocc();" The topic is that sometimes it fails and other it doesn´t...
I think we need to see more of the code to be able to help. $row = $result->fetch_assoc() will fetch each column unless you have specified only the date column in your MySQL query, for example, and if it is selecting all the data then some of those columns aren't dates and will throw errors when you try to use strtotime(). Can you update your original post to include ALL of the relevant code?
I´ve just pasted the complete code, the FOR sentence closes after the last line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.