Current Location: Home> Latest Articles> PHP Date and Time Handling: How to Format Current Date and Time Using the date Function

PHP Date and Time Handling: How to Format Current Date and Time Using the date Function

gitbox 2025-06-13

PHP Date and Time Handling: How to Format Current Date and Time Using the date Function

In PHP development, handling date and time is a common and crucial requirement. Whether displaying publication dates on a website or recording user actions, date-time formatting is essential. PHP offers a range of date-time handling functions, with the date function being one of the most widely used. This article will explain how to use the date function to format the current date and time and provide some common formatting examples.

Introduction to the date Function

The date function is used to format a Unix timestamp into a human-readable date and time string. The basic syntax is as follows:

string date ( string $format [, int $timestamp = time() ] )

In this syntax, the $format parameter specifies the date-time format, and the $timestamp parameter specifies the Unix timestamp. If the $timestamp parameter is not specified, the current time is used by default. The function returns the formatted date-time string.

Common Date-Time Formatting Examples

Here are a few common date-time formatting examples for your reference:

2.1 Formatting as Year-Month-Day

echo date("Y-m-d"); // Output: 2022-01-01

2.2 Formatting as Year-Month-Day Hour:Minute:Second

echo date("Y-m-d H:i:s"); // Output: 2022-01-01 12:34:56

2.3 Output Current Timestamp

echo time(); // Output current Unix timestamp

Explanation of Formatting Symbols

In the $format parameter, various formatting symbols can be used to represent different parts of the date-time. Below are some common formatting symbols and their meanings:

  • Y: Four-digit year, e.g., 2022;
  • m: Two-digit month, e.g., 01;
  • d: Two-digit day, e.g., 01;
  • H: Hour in 24-hour format, e.g., 12;
  • i: Minutes, e.g., 34;
  • s: Seconds, e.g., 56;
  • l: Full name of the weekday, e.g., Saturday;
  • W: Week number of the year, e.g., 1 for the first week.

More Date-Time Formatting Examples

In addition to the examples above, you can customize the date-time format further to meet your needs. Here are a few more examples:

4.1 Format Date-Time in Chinese Format

$timestamp = time();

echo date("Y年m月d日 H时i分", $timestamp); // Output: 2022年01月01日 12时34分

4.2 Display Only Month and Day

echo date("M j", time()); // Output: Jan 1

4.3 Display the Current Day of the Week

echo date("l", time()); // Output: Saturday

4.4 Calculate the Number of Days from a Specific Date

$date1 = strtotime("2022-01-01");

$date2 = time();

$days = ($date1-$date2)/60/60/24;

echo round($days); // Output: 0

These are just a few simple examples. You can perform more complex date-time formatting operations according to your specific requirements. Hopefully, these examples will help you better handle date and time in PHP.

Conclusion

This article explained how to use the date function in PHP to format the current date and time. Through common examples, we demonstrated how to customize the date format according to different needs. Date-time handling is an essential part of PHP development, and mastering the date function can significantly improve development efficiency.

We hope this article has been helpful. Thank you for reading!