You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 07:40:43