Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this error when inserting $diff into my database table
SQL
time_in(time)
but im having this error
Catchable fatal error: Object of class DateInterval could not be converted to string
the error is in my query

What I have tried:

$conn = mysqli_connect(server,username,password)
        or die (mysqli_error($conn));
mysqli_select_db($conn,database)
            or die (mysqli_error($conn));

if ($now->format("H:i") > "22:00") {
    $deadline = DateTime::createFromFormat("H:i", "22:00");
    $diff = $now->diff($deadline);
    echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
} else if ($now->format("H:i") > "19:00") {
    $deadline = DateTime::createFromFormat("H:i", "19:00");
    $diff = $now->diff($deadline);
    echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
} else if ($now->format("H:i") > "16:00") {
    $deadline = DateTime::createFromFormat("H:i", "16:00");
    $diff = $now->diff($deadline);
    echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
} else if ($now->format("H:i") > "13:00") {
    $deadline = DateTime::createFromFormat("H:i", "13:00");
    $diff = $now->diff($deadline);
    echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
} else if ($now->format("H:i") > "10:00") {
    $deadline = DateTime::createFromFormat("H:i", "10:00");
    $diff = $now->diff($deadline);
    echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
} else if ($now->format("H:i") > "07:00") {
    $deadline = DateTime::createFromFormat("H:i", "07:00");
    $diff = $now->diff($deadline);
    echo "You are ".$diff->h." hours and ".$diff->i." minutes late";
}

$query = "INSERT INTO `time_in`(`late`,`date_in`)
                VALUES ('$diff',NOW())";
                if (!$query) {
                printf("Error: %s\n", mysqli_error($conn));
                exit();
                } 
        if($query = $conn ->query($query)){
            echo "<br>successful";
        } else {
            echo "not successful";
        }
Posted
Updated 18-Feb-17 3:08am
Comments

1 solution

That is because
$diff
is a
DateInterval
object, not a string. To insert the string representation of the intervals in hours and minutes, do this:
$diff->format("%H:%i")

Do use PHP Prepared Statements[^] to avoid sql injection risk.
 
Share this answer
 
v3
Comments
Member 12986530 18-Feb-17 9:15am    
i change $diff->format("H:i") into $diff->format('H:i') because it giving me a error of Parse error: syntax error, unexpected 'H' (T_STRING) but when im changing it to single quote it give me this Notice: Undefined property: DateInterval::$format
Peter Leow 18-Feb-17 9:25am    
It should be
$diff->format("%H:%i")
Member 12986530 18-Feb-17 9:25am    
i change into
$diff->format("H:i")
because im getting this
Parse error: syntax error, unexpected 'H' (T_STRING)
so change it to
$diff->format('H:i')
but im still getting this
Notice: Undefined property: DateInterval::$format
Peter Leow 18-Feb-17 9:30am    
Solution corrected.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900