php date data type conversion

王林
Release: 2023-05-07 11:11:11
Original
825 people have browsed it

In developing web applications, conversion of date data types is often involved. PHP provides a wealth of date and time functions, which can easily perform date formatting, comparison, calculation and other operations. This article will introduce date data types in PHP and their conversion methods.

PHP date data type

In PHP, dates can be represented by strings or integers. Commonly used date formats include:

  • Y-m-d H:i:s: year-month-day hour: minute: second, such as 2022-01-01 00:00:00
  • Y/m/d H:i:s: year/month/day hour: minute: second, such as 2022/01/01 00:00:00
  • Y year m month d day H hour i minute s Seconds: year, month, day, hour, minute and second, such as 00:00:00 on January 1, 2022
  • Unix timestamp: the number of seconds elapsed since January 1, 1970, such as 1640995200

String to date type

The PHP built-in function strtotime() can be used to convert strings representing date and time into Unix timestamps. This function accepts a string representing a date and time as parameters and returns the Unix timestamp corresponding to the date.

$time_str = '2022-01-01';
$time_stamp = strtotime($time_str);
echo $time_stamp; // 输出:1640995200
Copy after login

The above code converts the date string '2022-01-01' into a Unix timestamp and outputs the timestamp.

Note: When using the strtotime() function to convert a date string into a timestamp, the date format must be the English date format of "Month Day Year" or "Day Month Year" or Date format of "Y-m-d".

Unix timestamp to date format

The Unix timestamp can be formatted into the specified date format through the PHP built-in function date(). This function accepts two parameters: the first parameter is a string representing the date format, and the second parameter is a Unix timestamp.

$time_stamp = 1640995200;
$date_str = date('Y-m-d H:i:s', $time_stamp);
echo $date_str; // 输出:2022-01-01 00:00:00
Copy after login

The above code formats the Unix timestamp 1640995200 into a date string in the 'Y-m-d H:i:s' format and outputs the string.

Direct string comparison

In PHP, you can directly use string comparison operators (>, <, ==, !=, etc.) to compare the size of date strings. If you are converting a date string to a Unix timestamp, you can also compare directly using numeric comparison operators (>, <, =, !=, etc.).

$date_str1 = '2022-01-01';
$date_str2 = '2022-01-02';
if ($date_str1 < $date_str2) {
    echo '日期 ' . $date_str1 . '在 ' . $date_str2 . '之前';
} else {
    echo '日期 ' . $date_str1 . '在 ' . $date_str2 . '之后';
}
Copy after login

The above code compares the sizes of two date strings and outputs the final comparison result.

Date to timestamp

You can use the PHP built-in function mktime() to convert dates to Unix timestamps. This function accepts multiple parameters, which are hours, minutes, seconds, months, days, and years. If no arguments are specified, the function returns the Unix timestamp of the current time.

$year = 2022;
$month = 1;
$day = 1;
$hour = 0;
$minute = 0;
$second = 0;
$time_stamp = mktime($hour, $minute, $second, $month, $day, $year);
echo $time_stamp; // 输出:1640995200
Copy after login

The above code converts the date '2022-01-01' into a Unix timestamp and outputs the timestamp.

Convert timestamp to date

Like the date() function introduced above, PHP's built-in function strftime() can also format Unix timestamps into the specified date format. This function accepts two parameters: the first parameter is a string representing the date format (supports formatting characters, such as %Y, %m, %d, %H, %M, %S, etc.), and the second parameter is Unix timestamp.

$time_stamp = 1640995200;
$date_str = strftime('%Y年%m月%d日 %H时%M分%S秒', $time_stamp);
echo $date_str; // 输出:2022年01月01日 00时00分00秒
Copy after login

The above code formats the Unix timestamp 1640995200 into the specified date format and outputs the formatted string.

Conclusion

In PHP, developers can use the built-in functions strtotime(), date(), mktime(), strftime() and other functions to easily perform conversion operations involving date types. operate. In actual development, it is necessary to select appropriate functions according to specific needs and use them flexibly.

The above is the detailed content of php date data type conversion. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!