0

I have a problem, i'm a beginnner I have a birthday date (in the format Y-m-d) And I have the actually date.

$date = $_POST["DatumJJJJ"]."-".$_POST["DatumMM"]."-".$_POST["DatumTT"];
$birthday = new DateTime($date);
$now = new DateTime(date("Y-m-d"));
$difference = $birthday->diff($now);
echo $difference;

Now, theres an error in the last line:

Catchable fatal error: Object of class DateInterval could not be converted to string

What should I do? I saw other similar questions but they don't help me!

Korne127
  • 168
  • 14
  • look the structure of dateInterval obect http://php.net/manual/en/class.dateinterval.php – splash58 Jun 08 '16 at 11:29
  • this is because `$difference` is object of try `$difference->format('%R%a days');` for more detail have a look at http://php.net/manual/en/datetime.diff.php – Chetan Ameta Jun 08 '16 at 11:30

1 Answers1

0

It's because $difference is not a string. You cannot print it directly like you print string. If you want to see the data contained by $difference. Use print_r(). Like this,

print_r($difference);

Output of the above function would be something like this,

DateInterval Object
(
    [y] => 0
    [m] => 7
    [d] => 29
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 242
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

See http://php.net/manual/en/class.dateinterval.php for detailed information of each key within object.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47