I'm having trouble converting a date Time object to a string, so I can pass it through a MySQLi prepared statement which specifies data types.
The error I'm receiving is:
Catchable fatal error: Object of class DateTime could not be converted to string
The line this error refers to is the execution of my prepared statement. I still receive this error despite many attempts at converting the Object to a string. Below I will paste the main 3 ways I've tried to tackle this issue.
1 - Using a casting operator:
$total_hours_calc = $time_diff->format('%h') + ($time_diff->format('%i'))/60;
$total_hours = (string)$total_hours_calc;
2 - Using Print_r:
$total_hours_calc = $time_diff->format('%h') + ($time_diff->format('%i'))/60;
$total_hours = print_r($total_hours_calc, true)
3 - Concatenation workaround:
$total_hours_calc = $time_diff->format('%h') + ($time_diff->format('%i'))/60;
$total_hours = $total_hours_calc . "";
All these attempts return the error stated at the beginning of this question.
Any help, solutions or magic PHP functions unknown to those who have yet to achieve a black belt in the art of PHP - are greatly appreciated.
[Requested] The Prepared statement -
$sql = "INSERT INTO shift ".
"(uniqueid, shift_date, start_time, end_time, total_hours, rate_of_pay, addedBy, paidRate, totalPaid) ".
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $connection->prepare($sql);
$stmt->bind_param('sssssssss', $uniqueid, $shift_date, $start_time, $end_time, $total_hours, $rate_of_pay, $addedBy, $paidRate, $totalPaid);
$stmt->execute();
The line referred to in all errors is: $stmt->execute();
$time_diff?date_diffof 2 times. I'm simply trying to convert the dateTime object to a string so it can be stored in database using MySQLi prepared statement. I can't store it as an integer as it can have a decimal point, so I need to store it as a string.$stmt->bind_param('sssssssss', $uniqueid, $shift_date, $start_time, $end_time, $total_hours, $rate_of_pay, $addedBy, $paidRate, $totalPaid);I assume that there are at least three vars looking like a DateTime Object:$shift_date,$start_timeand$end_time. Did you check all of them or how do you know its related to$total_hours?