php Object of class DateInterval could not be converted to string
You need to call DateInterval::format()
to display that difference as a string.
echo $diff->format('%d days');
See the manual for all of the available formatting options.
WHERE DateInterval could not be converted to string
$diff
is a DateInterval
object and thus cannot be interpolated or concatenated as a string as there is no string representation of it. I believe you are trying to interpolate the result from the format()
method of DateInterval.
Try:
"SELECT * FROM smt WHERE day = " . $diff->format('d');
This query here should be safe from SQL injection providing that $diff
is always a DateInterval object, however you should be using parameterized queries with MySQLi or PDO.
PHP Converting DateInterval to int
The error message appears because $months
is not an int, but a Datetime object like this one:
DateInterval Object
(
[y] => 0
[m] => 4
[d] => 12
[h] => 6
[i] => 56
[s] => 9
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 133
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
You can get the integer value of months like this
$due_date = new DateTime('13-02-2016');
$today = new DateTime();
$months = $due_date->diff($today);
echo $months->m;
Check the above result in PHP Sandbox
So basically your code will look like
$due_date = new DateTime($_POST['due_date']);
$today = new DateTime();
$months = $due_date->diff($today);
$fine = 0.02 * $price * $months->m; // i got no error in this line
$bill = $price + $fine;
I need to get the difference between two time stamps when I post comment on the page
public DateInterval::format ( string $format ) : string
https://www.php.net/manual/en/dateinterval.format.php
The format function (line 10), will return a string , so I'm not sure why you're using that to get the $seconds
Instead, date_diff
returns a DateInterval object, so you should look at the php docs for DateInterval
https://www.php.net/manual/en/class.dateinterval.php
And you may notice that it has some properties such as: $y
, $m
, etc.
So what you should do is actually something like $time_difference->s
.
PHP date_diff DateInterval could not be converted
Your problem here is that you use just %h
modifier.
Information in DateInterval
stored not like total amount of hours between dates, but amounts of all types of date/time.
So, if you compare in your example
time like between 2:00pm three days ago and 3:00pm today
you get one hour with %h
, yes. But you also get 3 days with %d
modifier.
So, I suppose, you just need to check:
$days = $SinceLastUpdate->format('%d');
if ($days < 3) { /* do stuff */ }
But in more simple way you can just compare timestamps - current and $DataUpdated
, if difference is greater than 3 days (3 * 86400) - do something:
$TodaysDate = new DateTime("now");
$du = new DateTime($DataUpdated);
if ($du->format('U') - $TodaysDate->format('U') > (86400 * 3)) {
/* do stuff */
}
Catchable fatal error: Object of class DateInterval could not be converted to string in C:\xampp\htdocs\testing.php on line 30
You want:
$diff = $diff->d;
Instead of:
$diff->format("%days");
Also note your code is vulnerable to SQL Injection, and if the dates aren't formatted you'll get errors from date_create/date_diff too!
Related Topics
File Not Found When Running PHP with Nginx
PHP Allocate Color Without Image Resource
Strip PHP Variable, Replace White Spaces with Dashes
Convert a JSON into a Utf-8 String
Send Email with a Template Using PHP
Using Preg_Split with Multiple Spaces
How to Post a Large Amount of Data Within PHP Curl Without Memory Overhead
Laravel Use Same Form for Create and Edit
Why Are Escape Characters Being Added to the Value of the Hidden Input
How to Block 100,000+ Individual Ip Addresses
Decoding Numeric HTML Entities via PHP
Securing a Rest API Accessible from Android
How to Keep the Chinese or Other Foreign Language as They Are Instead of Converting Them into Codes