PHP is a server-side scripting language widely used in website development. It provides many functions for processing dates, which can easily convert dates into arrays. This article will introduce how to convert date to array using PHP.
PHP provides the strtotime function, which can convert a date string into a timestamp in the format of a Unix timestamp. For example, to convert "2021-08-05" to a timestamp, you can use the following code:
$dateString = "2021-08-05"; $timestamp = strtotime($dateString);
PHP provides The getdate function can convert the timestamp into an array, which contains information such as year, month, day, week, etc. For example, to convert the timestamp $timestamp to an array, you can use the following code:
$dateArray = getdate($timestamp);
Here is a complete sample code to convert a date to an array:
"; echo "月份:" . $dateArray["mon"] . "
"; echo "日期:" . $dateArray["mday"] . "
"; echo "星期:" . $dateArray["wday"] . "
"; ?>
PHP provides convenient functions to convert date strings to timestamps, or timestamps to arrays, which we can choose to use according to our needs These functions facilitate date processing and use.
Through the introduction of this article, we have learned how to convert dates into arrays in PHP. I believe this will be helpful for us when processing dates.
The above is the detailed content of How to convert date to array using PHP. For more information, please follow other related articles on the PHP Chinese website!