Home  >  Article  >  Backend Development  >  PHP function application returns the day before and the day after a certain date

PHP function application returns the day before and the day after a certain date

青灯夜游
青灯夜游Original
2021-08-19 18:41:343205browse

In the previous article "Use of PHP function to calculate how many days there are until a certain day of a certain month of a certain year", we introduced the use of strtotime() function to calculate the time difference between two given dates. method. This time we will take a look at how to return the date before and after a specified date. Interested friends can learn about it~

The focus of this article is: returning the date of the day before and the day after a given time. So how to do it?

It’s actually very simple. PHP’s built-in strtotime() function can achieve this operation! Let’s take a look at my implementation method:

  • Returns the implementation code of the day before a certain date

<?php
function GetTime($year,$month,$day){
	$timestamp = strtotime("{$year}-{$month}-{$day}");

	$time = strtotime("-1 days",$timestamp);
	echo date("Y-m-d",$time)."<br>";
}
GetTime(2000,3,1);
GetTime(2021,1,1);
?>

Output result:

PHP function application returns the day before and the day after a certain date

  • Return the implementation code of the day after a certain date

<?php
function GetTime($year,$month,$day){
	$timestamp = strtotime("{$year}-{$month}-{$day}");

	$time = strtotime("+1 days",$timestamp);
	echo date("Y-m-d",$time)."<br>";
}
GetTime(2000,2,28);
GetTime(2021,2,28);
?>

Output result:

PHP function application returns the day before and the day after a certain date

Analyze the key code:

  • strtotime() The function has two uses: one is to parse the date and time described in English text in the form of a string into a UNIX timestamp, and the other is to calculate the interval between some dates and times.

  • We use the strtotime() function to calculate the time interval, using strtotime("-1 days",$timestamp) and strtotime(" 1 days",$timestamp)<br>Calculate the date before and after the specified date.

    "-1 days" means subtracting one day, " 1 days" means adding one day; observing the pattern, we can also get the first N days and the next N days as needed The date of the day

<?php
function GetTime($year,$month,$day){
	$timestamp = strtotime("{$year}-{$month}-{$day}");

	$time1 = strtotime("-2 days",$timestamp);
	$time2 = strtotime("+3 days",$timestamp);
	
	echo date("Y-m-d",$time1)."<br>";
	echo date("Y-m-d",$time2)."<br>";
}
GetTime(2000,3,5);
?>

PHP function application returns the day before and the day after a certain date

  • When the strtotime() function has two parameters, the second parameter must be a timestamp Format. Therefore, we need to use the strtotime() function once to convert the specified date in string form into a string; then use the strtotime() function once to perform the addition and subtraction of dates to obtain the dates N days before and N days after.

  • The return value of the strtotime() function is in timestamp format; so you need to use date("Y-m-d",$time) to format the date and time and return Year-month-day format date.

Expand knowledge:

  • In fact, using the strtotime() function, you can not only obtain the previous N days and The date of the next N days, you can also get the date of the previous N months and the next N months, the date of the previous N years and the next N years:

<?php
	$month1 = strtotime("-1 months",strtotime("2000-1-2"));
	$month2 = strtotime("+2 months",strtotime("2000-1-2"));
	echo date("Y-m-d",$month1)."<br>";
	echo date("Y-m-d",$month2)."<br><br>";
	
	$year1 = strtotime("-1 years",strtotime("2000-1-2"));
	$year2 = strtotime("+2 years",strtotime("2000-1-2"));
	echo date("Y-m-d",$year1)."<br>";
	echo date("Y-m-d",$year2)."<br>";
?>

Output result:

PHP function application returns the day before and the day after a certain date

  • If you want to get the dates of the previous week and the next week, you can also use the strtotime() function. For example: the current date is 2021-8-19, the dates of the previous week and the next week are:

PHP function application returns the day before and the day after a certain date

Implementation code:

<?php
header("content-type:text/html;charset=utf-8");
$start = time();  //获取当前时间的时间戳
echo "当前日期为:".date(&#39;Y-m-d&#39;,$start)."<br />";
$interval = 7 * 24 * 3600;  //一周总共的秒数
$previous_week = $start - $interval;  //当前时间的时间戳 减去  一周总共的秒数
$next_week = $start + $interval;  //当前时间的时间戳 加上  一周总共的秒数
echo "前一周日期为:".date(&#39;Y-m-d&#39;,$previous_week)."<br />";
echo "后一周日期为:".date(&#39;Y-m-d&#39;,$next_week)."<br />";
?>

Output result:

PHP function application returns the day before and the day after a certain date

#The difference between the two dates is exactly 7 days. This is actually a reverse application of calculating time difference.

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of PHP function application returns the day before and the day after a certain date. 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