How to save the interval duration of time in database. What type do I need to select: string
or timedate
?
I just take last data in MySQL (time on) and just need to save the duration went it turn off.
// error in the line below
$sendresult = $interval->format('%y yr %d dy %H hr %i mn %s sc');
$host = "localhost";
$user = "…";
$pass = "…";
$db = "…";
$con = mysqli_connect($host, $user, $pass, $db);
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$username="sam";
$id="01";
$date = date('Y-m-d H:i:s');
$statusdevide="off";
$result = mysqli_query($con,"SELECT * FROM SPA02 ORDER BY Date DESC LIMIT 1");
$printdata=mysqli_fetch_row($result);
$datalpas=new DateTime($printdata[2]);
$interval = $datalpas->diff($datetime2);
$sendresult=$interval->format('%y yr %d dy %H hr %i mn %s sc');//00 years 0 months 0 days 08 hours 0 minutes 0 seconds
$sql = "INSERT INTO SPA02 (User, Id, Date,Status,Duration)
VALUES ('$username', '$id', '$date','$statusdevide','$sendresult')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
What I expect is to save the duration time in database, instead I get:
1. Warning: DateTime::diff() expects parameter 1 to be DateTimeInterface,
2. PHP Fatal error: Uncaught Error: Call to a member function format() on boolean