php 如何将日期时间转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10048665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Convert DateTime to String?
提问by Jeremy Harris
I have an array of DateTime objects and need to use them as strings in a function call. I have tried casting it like $string_datetime = (string)$myDateTimeObject;but that doesn't work. Searching has been unfruitful as well, as most people are asking how to convert a string to DateTime instead.
我有一个 DateTime 对象数组,需要在函数调用中将它们用作字符串。我试过像这样铸造它,$string_datetime = (string)$myDateTimeObject;但这不起作用。搜索也没有结果,因为大多数人都在问如何将字符串转换为 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 which expects a string (it's an API call) I receive this error:
当调用一个需要字符串的函数(它是一个 API 调用)时,我收到这个错误:
Catchable fatal error: Object of class DateTime could not be converted to string
可捕获的致命错误:无法将 DateTime 类的对象转换为字符串
What's the correct way of converting/extracting a string from a DateTime object?
从 DateTime 对象转换/提取字符串的正确方法是什么?

