Home >Backend Development >PHP Problem >How to determine the day of the year that a specified date is in php
Judgment method: 1. Use the strtotime() function to convert the specified date into a timestamp format, with the syntax "strtotime("specified date")"; 2. Use the date() function to calculate whether the specified timestamp is in the current year. The day of the month, the syntax is "date("z", timestamp)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the date() function To calculate the day of the year for a specified date.
The date() function can specify a timestamp and convert it to the day of the year.
Syntax:
date(format,timestamp);
When the parameter format
is set to z
, the specified timestamp can be calculated timestamp
It is the day of the year. Note: Days start from 0 (from 0 to 365).
Example: Calculate the day of the year for the specified date 2018-01-18
<?php header("Content-type:text/html;charset=utf-8"); // 设置时区 date_default_timezone_set("PRC"); $time = strtotime("2018-01-18"); // 将指定日期转成时间戳 $date=date("z",$time); $date=$date+1; echo "是一年的第 ".$date." 天"; ?>
Output:
Analysis:
Because the date() function processes timestamps, you need to first use strtotime() to convert the specified date into timestamp format;
Then use date("z",$time)
To calculate the day of the year when the specified date is.
Because the number of days returned by date("z",$time) starts from 0, just add 1 to the return value.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine the day of the year that a specified date is in php. For more information, please follow other related articles on the PHP Chinese website!