使用 PHP 获取第二天和前一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5883571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get next and previous day with PHP
提问by tonoslfx
I have got two arrows set up, click for next day, next two days, soon and previous day, two days ago, soon. the code seem not working? as it only get one next and previous day.
我已经设置了两个箭头,点击第二天,接下来的两天,很快和前一天,两天前,很快。代码似乎不起作用?因为它只会在下一天和前一天得到一个。
<a href="home.php?date=<?= date('Y-m-d', strtotime('-1 day', strtotime($date))) ?>" class="prev_day" title="Previous Day" ></a>
<a href="home.php?date=<?= date('Y-m-d', strtotime('+1 day', strtotime($date))) ?>" class="next_day" title="Next Day" ></a>
is there a way if i click the next button, the date will continously change for the next day. for a moment it will only get one day ahead
有没有办法,如果我点击下一步按钮,第二天的日期会不断变化。有那么一刻它只会提前一天
回答by Alan Whitelaw
date('Y-m-d', strtotime('+1 day', strtotime($date)))
Should read
应该读
date('Y-m-d', strtotime(' +1 day'))
Update to answer question asked in comment about continuously changing the date.
更新以回答有关不断更改日期的评论中提出的问题。
<?php
$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
$next_date = date('Y-m-d', strtotime($date .' +1 day'));
?>
<a href="?date=<?=$prev_date;?>">Previous</a>
<a href="?date=<?=$next_date;?>">Next</a>
This will increase and decrease the date by one from the date you are on at the time.
这将从您当时所在的日期增加和减少日期。
回答by Erald Karakashi
Requirement: PHP 5 >= 5.2.0
要求:PHP 5 >= 5.2.0
You should make use of the DateTimeand DateIntervalclasses in Php, and things will turn to be very easy and readable.
您应该使用PHP中的DateTime和DateInterval类,事情将变得非常容易和可读。
Example: Lets get the previous day.
示例:让我们得到前一天。
// always make sure to have set your default timezone
date_default_timezone_set('Europe/Berlin');
// create DateTime instance, holding the current datetime
$datetime = new DateTime();
// create one day interval
$interval = new DateInterval('P1D');
// modify the DateTime instance
$datetime->sub($interval);
// display the result, or print_r($datetime); for more insight
echo $datetime->format('Y-m-d');
/**
* TIP:
* if you dont want to change the default timezone, use
* use the DateTimeZone class instead.
*
* $myTimezone = new DateTimeZone('Europe/Berlin');
* $datetime->setTimezone($myTimezone);
*
* or just include it inside the constructor
* in this form new DateTime("now", $myTimezone);
*/
References: Modern PHP, New Features and Good Practices By Josh Lockhart
参考资料:Josh Lockhart 的现代 PHP、新功能和良好实践
回答by Pandurang Zambare
Use
用
$time = time();
For previous day -
对于前一天 -
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)- 1 ,date("Y", $time)));
For 2 days ago
2 天前
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time) -2 ,date("Y", $time)));
For Next day -
第二天 -
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)+ 1 ,date("Y", $time)));
For next 2 days
未来 2 天
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time) +2 ,date("Y", $time)));
回答by Prajwal GN
strtotime('-1 day', strtotime($date))
This returns the number of difference in seconds of the given date and the $date.so you are getting wrong result .
这将返回给定日期和 $date.so 你得到错误结果的秒数差异。
Suppose $date is todays date and -1 day means it returns -86400 as the difference and the when you try using date you will get 1969-12-31 Unix timestamp start date.
假设 $date 是今天的日期,-1 天意味着它返回 -86400 作为差异,当您尝试使用 date 时,您将获得 1969-12-31 Unix 时间戳开始日期。
回答by SarwarCSE
Simply use this
简单地使用这个
echo date('Y-m-d',strtotime("yesterday"));
echo date('Y-m-d',strtotime("tomorrow"));
回答by shadyyx
it is enough to call it this way:
可以这样称呼它就足够了:
<a href="home.php?date=<?= date('Y-m-d', strtotime('-1 day')) ?>" class="prev_day" title="Previous Day" ></a>
<a href="home.php?date=<?= date('Y-m-d', strtotime('+1 day')) ?>" class="next_day" title="Next Day" ></a>
Also see the documentation.
另请参阅文档。
回答by Santosh Shah
always make sure to have set your default timezone
始终确保设置了默认时区
date_default_timezone_set('Europe/Berlin');
create DateTime instance, holding the current datetime
创建 DateTime 实例,保存当前日期时间
$datetime = new DateTime();
create one day interval
创建一天间隔
$interval = new DateInterval('P1D');
modify the DateTime instance
修改 DateTime 实例
$datetime->sub($interval);
display the result, or print_r($datetime);
for more insight
显示结果,或print_r($datetime);
了解更多信息
echo $datetime->format('Y-m-d');
TIP:
提示:
If you don't want to change the default timezone, use the DateTimeZone
class instead.
如果您不想更改默认时区,请改用DateTimeZone
该类。
$myTimezone = new DateTimeZone('Europe/Berlin');
$datetime->setTimezone($myTimezone);
or just include it inside the constructor in this form new DateTime("now", $myTimezone);
或者只是以这种形式将它包含在构造函数中 new DateTime("now", $myTimezone);
回答by Alex Joe
You could use 'now'as string to get today's/tomorrow's/yesterday's date:
您可以使用'now'作为字符串来获取今天/明天/昨天的日期:
$previousDay = date('Y-m-d', strtotime('now - 1day'));
$toDay = date('Y-m-d', strtotime('now'));
$nextDay = date('Y-m-d', strtotime('now + 1day'));
回答by Ponmathavan Mani
Php script -1****its to Next Date
Php 脚本 -1**** 到下一个日期
<?php
$currentdate=date('Y-m-d');
$date_arr=explode('-',$currentdate);
$next_date=
Date("Y-m-d",mktime(0,0,0,$date_arr[1],$date_arr[2]+1,$date_arr[0]));
echo $next_date;
?>**
**Php script -1****its to Next year**
<?php
$currentdate=date('Y-m-d');
$date_arr=explode('-',$currentdate);
$next_date=
Date("Y-m-d",mktime(0,0,0,$date_arr[1],$date_arr[2],$date_arr[0]+1));
echo $next_date;
?>
回答by Yosafat Ksatria
just in case if you want next day or previous day from today's date
以防万一,如果您想要从今天的日期开始的第二天或前一天
date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-1,date("Y")));
date("Ymd", mktime(0, 0, 0, date("m"),date("d")-1,date("Y")));
just change the "-1" to the "+1" regards, Yosafat
只需将“-1”更改为“+1”问候,Yosafat