Home > Backend Development > PHP Problem > How to determine the day of the year that a specified date is in php

How to determine the day of the year that a specified date is in php

青灯夜游
Release: 2023-03-14 07:52:01
Original
3611 people have browsed it

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)".

How to determine the day of the year that a specified date is in php

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);
Copy after login

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

Output:

How to determine the day of the year that a specified date is in php

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template