Home >Backend Development >PHP Problem >How to determine the day of the year based on the year, month and day in php

How to determine the day of the year based on the year, month and day in php

青灯夜游
青灯夜游Original
2022-04-22 17:02:312690browse

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.

How to determine the day of the year based on the year, month and day in php

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." 天";
?>

How to determine the day of the year based on the year, month and day in php

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!

Statement:
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
Previous article:What is 302 error in phpNext article:What is 302 error in php