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_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



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
answered 5 Months ago

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
answered 9 Months ago

The function json_decode does not return a string.

Have a look at the documentation for the function.

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.

Saturday, May 29, 2021
answered 7 Months ago

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
answered 7 Months ago

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
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 :