How is the function usage of PHP strtotime?

The strtotime function in PHP is a date/time function used to parse a human-readable date/time description into a Unix timestamp.

The usage of the strtotime function is as follows:

strtotime(string $time, int $now = time()): int|false
  1. $time: this represents the date/time string to be parsed. It can be an absolute date, such as “2021-01-01”, or a relative date, such as “tomorrow” for tomorrow, “+1 week” for a week later.
  2. $now (optional): Represents the base time used to calculate relative dates. By default, it is the current Unix timestamp.

The function returns the parsed Unix timestamp, or false if parsing fails.

Here are some common usage examples of the strtotime function:

echo strtotime("now");  // 当前时间的Unix时间戳

echo strtotime("2021-01-01");  // 指定日期的Unix时间戳

echo strtotime("tomorrow");  // 明天的Unix时间戳

echo strtotime("+1 week");  // 一周后的Unix时间戳

echo strtotime("next Monday");  // 下个周一的Unix时间戳

echo strtotime("last day of next month");  // 下个月最后一天的Unix时间戳

echo strtotime("2021-01-01 12:00:00");  // 指定日期时间的Unix时间戳

The strtotime function is very convenient for handling date and time strings, as it can quickly parse various formats of date and time descriptions.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds