Judgment method: 1. Use the "strtotime("year-month-day")" statement to convert the given year, month and day into a timestamp format; 2. Use "date("z", timestamp ) 1" statement calculates the day of the year for the specified timestamp. The number of days returned by date() is calculated from 0, so the real number of days needs to be added to this basis by 1.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php is based on the year and month Determining the day of the year to determine the day of the year
In PHP, you can use the date() function to determine the day of the year for a specified date.
But the date() function processes timestamps, so you need to first use the strtotime() function to convert the year, month and day into timestamp format. The calculation syntax of the
date() function:
date("z",时间戳);
The date() function can be used with the character "z" to format a timestamp and calculate the day of the year that the timestamp is. The range of the return value: from 0 to 365
Because the number of days returned is calculated from 0, the real number of days needs to be added to this basis by 1.
Implementation code:
<?php header("Content-type:text/html;charset=utf-8"); // 设置时区 date_default_timezone_set("PRC"); $str="2020-01-18"; $time = strtotime($str); // 将指定日期转成时间戳 $date=date("z",$time)+1; echo $str."是一年的第 ".$date." 天"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine the day of the year based on the year, month and day in php. For more information, please follow other related articles on the PHP Chinese website!