PHP Date Function: Usage & Examples

The date() function is used to format dates and times, and it returns a formatted date string. It takes two parameters, the first being the date format and the second being an optional timestamp.

// 获取当前日期时间
echo date("Y-m-d H:i:s");

// 使用时间戳获取指定日期时间
echo date("Y-m-d H:i:s", strtotime("2022-01-01"));

// 获取星期几
echo date("l");

// 获取时间戳
echo time();

The code example above demonstrates how to use the date() function to retrieve the current date and time, specify a date, determine the day of the week, and obtain a timestamp. You can customize the date format according to your needs to get different date and time formats.

bannerAds