如何在PHP中将日期时间转换为Firebase Timestamp格式?
解决Firebase「无效时间戳」错误的PHP日期格式转换方案
首先明确Firebase数据库接受的有效时间戳格式:
- RFC3339标准格式:例如
2023-02-24T14:00:00Z(UTC时区) - UNIX时间戳:秒级(如
1677223200)或毫秒级(如1677223200000)
你的代码问题在于把原本符合标准的RFC3339格式转成了Firebase不识别的m/d/Y H:i A格式(比如02/24/2023 14:00 PM),导致导入时触发「无效时间戳」错误。
修正方案1:直接使用原始RFC3339格式
你的$booking->format_start_date_and_time_rfc3339()方法已经返回了符合Firebase要求的RFC3339格式字符串,不需要额外转换,直接赋值即可:
$vars = [ 'id' => $booking->id, 'booking_code' => $booking->booking_code, 'start_datetime' => $booking->format_start_date_and_time_rfc3339(), 'end_datetime' => $booking->format_end_date_and_time_rfc3339(), // 其他字段... ];
修正方案2:转换为UNIX时间戳(秒级)
如果Zapier或Webhooks更适配数字型时间戳,可将日期转成秒级UNIX时间戳:
// 开始时间转换 $start_rfc3339 = $booking->format_start_date_and_time_rfc3339(); $start_timestamp = strtotime($start_rfc3339); // 结束时间转换 $end_rfc3339 = $booking->format_end_date_and_time_rfc3339(); $end_timestamp = strtotime($end_rfc3339); $vars = [ 'id' => $booking->id, 'booking_code' => $booking->booking_code, 'start_datetime' => $start_timestamp, 'end_datetime' => $end_timestamp, // 其他字段... ];
注意事项
- 避免使用
m/d/Y H:i A这类自定义格式,Firebase无法自动解析这种格式的字符串为时间戳 - 如果需要指定时区,确保RFC3339字符串包含时区信息(比如末尾的
Z代表UTC,或+08:00代表东八区)
内容的提问来源于stack exchange,提问作者Jamie Warren
相关产品推荐
相关产品推荐

