Asked  5 Months ago    Answers:  5   Viewed   76 times

i've tried using date_diff and date_create to get a difference from two date that's already converted to string.

here's the code:

$date_1 = date_create();
$date_now = date_format($date_1, 'Y-m-d');


//echo $date_now . "n";
$date=date_create($date_now);
date_add($date,date_interval_create_from_date_string("3 days"));
$date_return =  date_format($date,"Y-m-d");


$diff = date_diff(date_create($date_now), date_create($date_return));

echo $diff;

and i am getting this error:

Object of class DateInterval could not be converted to string

 Answers

24

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.

Wednesday, July 7, 2021
 
mattltm
answered 5 Months ago
76

The problem is that the end date/time itself is never included in the period -- that is, the DatePeriod object represents date/times in the [$startDate, $endDate) range¹.

You have to add one second at least to the end date to always get the expected results. But let's not be stingy and add a whole minute:

$days = new DatePeriod(
    new DateTime("first $day of $m $y"),
    DateInterval::createFromDateString('next ' . $day),
    new DateTime("last day of $m $y 00:01")
);

¹ unless you use the DatePeriod::EXCLUDE_START_DATE option, in which case the start date itself is excluded as well

Wednesday, March 31, 2021
 
Gregosaurus
answered 9 Months ago
74

The function json_decode does not return a string.

Have a look at the documentation for the function.

http://fr2.php.net/function.json-decode.php

The problem is that you're trying to echo the object. The following code is faulty.

echo $issue_json;

You can use var_dump to see the structure of the object returned from json_decode.

var_dump($issue_json);
Saturday, May 29, 2021
 
KHM
answered 7 Months ago
KHM
41

I'm not sure what format you're looking for in your difference but here's how to do it using DateTime

$datetime1 = new DateTime();
$datetime2 = new DateTime('2011-01-03 17:13:00');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $elapsed;
Friday, June 4, 2021
 
Jesse
answered 7 Months ago
68

Most likely, the userdata() function is returning an object, not a string. Look into the documentation (or var_dump the return value) to find out which value you need to use.

Thursday, June 24, 2021
 
weegee
answered 6 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share