php如何进行时间的格式转换

2025-11-10 08:43:15

1、打开PHPstudy,开启集成开发环境,创建test.php文件,并用notepad++打开。

php如何进行时间的格式转换

php如何进行时间的格式转换

php如何进行时间的格式转换

2、首先是最常用的时间戳与规定时间格式之间的互相转换:

//获取时间戳

$time = time();

echo $time;

echo '<br>';

//转换为年月日时分秒格式

$formatTime=date('Y-m-d H:i:s',time());

echo $formatTime;

echo '<br>';

$nowtime=strtotime($formatTime);

echo $nowtime;

php如何进行时间的格式转换

3、打开浏览器可以发现我们将时间戳转换成年月日时分秒格式的时间了,又把年月日时分秒格式的时间转换成时间戳了。

php如何进行时间的格式转换

4、然后是一些常用的获取时间戳方式:

echo "一个小时后的时间戳:".strtotime("+1 hours")."<br>";

echo "明天的时间戳:".strtotime("+1 day")."<br>";

echo "下星期的时间戳:".strtotime("+1 week")."<br>";

echo "下星期日的时间戳:".strtotime("next Sunday")."<br>";

echo "一个小时前的时间戳:".strtotime("-1 hours")."<br>";

echo "咋天的时间戳:".strtotime("-1 day")."<br>";

echo "上星期的时间戳:".strtotime("-1 week")."<br>";

echo "上星期日的时间戳:".strtotime("last Sunday");

php如何进行时间的格式转换

5、访问浏览器可以看到输出的时间戳。

php如何进行时间的格式转换

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢