牛牛在线工具

DateTimeInterface::getTimestamp()

收录 8868 个 PHP 函数,含用途说明与用法示例。

DateTimeInterface::getTimestamp()

获取DateTime对象的Unix时间戳

函数名:DateTimeInterface::getTimestamp() 适用版本:PHP 5 >= 5.3.0, PHP 7 用法:该函数用于获取DateTime对象的Unix时间戳。它返回一个整数,表示自1970年1月1日00:00:00以来经过的秒数。 示例代码: // 创建一个DateTime对象,表示当前时间 $datetime = new DateTime(); // 获取当前时间的Unix时间戳 $timestamp = $datetime->getTimestamp(); // 输出时间戳 echo $timestamp; 示例说明:上述示例代码中,首先创建了一个DateTime对象,代表当前时间。然后使用getTimestamp()方法获取当前时间的Unix时间戳,并将结果赋值给$timestamp变量。最后,使用echo输出这个时间戳。 注意事项: DateTimeInterface::getTimestamp()方法只能用于实现了DateTimeInterface接口的类(如DateTime类)的实例对象上。 请确保在调用getTimestamp()方法之前,已经正确设置了日期和时间。否则,该方法将返回UNIX时间戳的初始值0。