How can I convert DateTime to string?

I have an array of DateTime objects and need to use them as strings in a function call. I tried to do it like $string_datetime = (string)$myDateTimeObject;

, but it doesn't work. The search was fruitless as well as most people ask how to convert a string to DateTime.

My code:

$start_date = new DateTime();
$end_date = new DateTime();
$end_date = $end_date->modify('+1 day');

// Add time range to request
$request['time_range'] = Array ( 'start' => $start_date,
                                 'end'   => $end_date);

      

When calling a function that expects a string (this is an API call), I get this error:

Allowable fatal error: DateTime object cannot be converted to string

What is the correct way to convert / extract a string from a DateTime object?

+3


source to share


1 answer


Use DateTime::format()

.



+6


source







All Articles