DateTimeInterface::getTimestamp DateTimeImmutable::getTimestamp DateTime::getTimestamp date_timestamp_get 获取 Unix 时间戳 &reftitle.description; &style.oop; public intDateTimeInterface::getTimestamp public intDateTimeImmutable::getTimestamp public intDateTime::getTimestamp &style.procedural; intdate_timestamp_get DateTimeInterfaceobject 获取 Unix 时间戳。 &reftitle.parameters; &no.function.parameters; &reftitle.returnvalues; 返回表示日期的 Unix 时间戳。 &reftitle.errors; 如果时间戳不能表示为 &integer;,将抛出 DateRangeError。在 PHP 8.3.0 之前,将抛出 ValueError。并且在 PHP 8.0.0 之前,在这种情况下返回 &false;。不过,可以使用 U 格式和 DateTimeInterface::format 作为 &string; 检索时间戳。 &reftitle.changelog; &Version; &Description; 8.3.0 超出范围的异常现在是 DateRangeError 8.0.0 这些函数在失败时不再返回 &false;。 &reftitle.examples; <function>DateTime::getTimestamp</function> 示例 &style.oop; getTimestamp(); ]]> &example.outputs.similar; &style.procedural; &example.outputs.similar; 如果需要以毫秒或微秒精度检索时间戳,则可以使用 DateTimeInterface::format 函数。 以毫秒和微秒精度检索时间戳 &style.oop; format('Uv'); // Timestamp in milliseconds $micro = (int) $date->format('Uu'); // Timestamp in microseconds echo $milli, "\n", $micro, "\n"; ]]> &example.outputs.similar; &reftitle.seealso; DateTime::setTimestamp DateTimeImmutable::setTimestamp DateTimeInterface::format