Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

datetime - php Object of class DateInterval could not be converted to string

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 . "
";
$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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...