php支持动态的调用方法,这点很不错,在使用carbon的时候我想要知道所有的输出格式,如果一个一个测试,太慢,所以想到了批量测试,代码如下:
.....
use Carbon\Carbon;
....
$data = TeacherRestSchedule::find($id);
$data['start_time'] = Carbon::createFromFormat('Y-m-d H:i:s', $data['start_time'])->toIso8601String();
$class = new \ReflectionClass('Carbon\Carbon');
$methods = $class->getMethods();
foreach ($methods as $key => $value) {
$hasMethod[] = $value->name;
if (ends_with($name, 'String') && starts_with($name, 'to')) {
echo $name . '() ---输出格式为:' . Carbon::now()->$name() . '<br>';
}
}
dd($hasMethod);
输出结果如下:
toDateString() —输出格式为:2018-06-07
toFormattedDateString() —输出格式为:Jun 7, 2018
toTimeString() —输出格式为:07:50:15
toDateTimeString() —输出格式为:2018-06-07 07:50:15
toDayDateTimeString() —输出格式为:Thu, Jun 7, 2018 7:50 AM
toAtomString() —输出格式为:2018-06-07T07:50:15+00:00
toCookieString() —输出格式为:Thursday, 07-Jun-2018 07:50:15 UTC
toIso8601String() —输出格式为:2018-06-07T07:50:15+00:00
toRfc822String() —输出格式为:Thu, 07 Jun 18 07:50:15 +0000
toIso8601ZuluString() —输出格式为:2018-06-07T07:50:15Z
toRfc850String() —输出格式为:Thursday, 07-Jun-18 07:50:15 UTC
toRfc1036String() —输出格式为:Thu, 07 Jun 18 07:50:15 +0000
toRfc1123String() —输出格式为:Thu, 07 Jun 2018 07:50:15 +0000
toRfc2822String() —输出格式为:Thu, 07 Jun 2018 07:50:15 +0000
toRfc3339String() —输出格式为:2018-06-07T07:50:15+00:00
toRssString() —输出格式为:Thu, 07 Jun 2018 07:50:15 +0000
toW3cString() —输出格式为:2018-06-07T07:50:15+00:00
toRfc7231String() —输出格式为:Thu, 07 Jun 2018 07:50:15 GMT
array:313 [▼
0 => "setHumanDiffOptions"
1 => "enableHumanDiffOption"
2 => "disableHumanDiffOption"
3 => "getHumanDiffOptions"
4 => "useMicrosecondsFallback"
5 => "isMicrosecondsFallbackEnabled"
6 => "useMonthsOverflow"
7 => "resetMonthsOverflow"
8 => "shouldOverflowMonths"
9 => "useYearsOverflow"
10 => "resetYearsOverflow"
11 => "shouldOverflowYears"
12 => "compareYearWithMonth"
13 => "shouldCompareYearWithMonth"
14 => "safeCreateDateTimeZone"
15 => "__construct"
16 => "instance"