互联网技术 · 2024年2月21日

PHP如何使用date()函数来格式化时间显示

这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下。

date() 函数

功能:用于格式化时间,返回一个字符串。

语法:string date( string format [, int timestamp] ),其中参数 format 表示时间格式化的方式;可选参数timestamp表示时间戳,默认为time(),即当前时间。

时间格式化的方式

PHP如何通过date() 函数格式化显示时间

例子一

date(“Y-m-d”); //显示格式如 2008-12-01

date(“Y-m-d”,time()); //显示格式如 2008-12-01

date(“Y.m.d”,time()); //显示格式如 2008.12.01

date(“M d Y”,time()); //显示格式如 Dec 01 2008

date(“Y-m-d H:i”,time()); //显示格式如 2008-12-01 12:01

例子二

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册