简体   繁体   English

如何获得4天前到现在 - php

[英]how to get 4-days ago to now - php

If current date is 2016-03-06, I would like to get these dates : 如果当前日期是2016-03-06,我想获得这些日期:

2016-03-06
2016-03-05
2016-03-04
2016-03-03

I'm trying to get this purpose but my result not what I want : 我试图达到这个目的,但我的结果不是我想要的:

$_4date = date("y-m-d",strtotime("day"));
$_3date = date("y-m-d",strtotime("-1 day"));
$_2date = date("y-m-d",strtotime("-2 day"));
$_1date = date("y-m-d",strtotime("-3 day"));

echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;

the result is : 结果是:

70-01-01
16-03-05
16-03-04
16-03-03

To get today's date with strtotime , you do strtotime("today"); 要用strtotime获取今天的日期,你要做strtotime("today"); . However, as Bjorn has commented, you can simply just call date() directly. 但是,正如Bjorn所评论的那样,您只需直接调用date()即可。

Furthermore, the reason you are not getting the year in four digits is because you are using a lowercase y instead of an uppercase Y . 此外,你没有得到四位数的年份是因为你使用小写y而不是大写Y

Try date("Ymd", strtotime("-1 day")); 尝试date("Ymd", strtotime("-1 day")); .

The following piece of code illustrates the required changes: 以下代码说明了所需的更改:

$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));

echo "$today <br />";
echo "$yesterday <br />";

// Output
2016-03-06 
2016-03-05

For more informtation, please consult the PHP documentation on the date function . 有关更多信息,请参阅日期函数PHP文档 It actually shows you that what to expect from y and Y and it also shows you that the default value that is passed as the second argument is time() , meaning the default is the current time. 它实际上向您显示了对yY期望,并且它还向您显示作为第二个参数传递的默认值是time() ,这意味着默认值是当前时间。

PHP's strtotime documentation can be consulted for more information on the strtotime() function and its possible parameters. 有关strtotime()函数及其可能参数的更多信息,可以参考PHP的strtotime文档

Always check the (PHP) documentation first before asking a question. 在提出问题之前,请先检查(PHP)文档。

You need to use like that: 你需要这样使用:

$_4date = date("Y-m-d"); 
$_3date = date("Y-m-d",strtotime("-1 day")); 

$_2date = date("Y-m-d",strtotime("-2 day")); 
$_1date = date("Y-m-d",strtotime("-3 day"));

Explanation : 说明

For current date no need to use use strtotime() . 对于当前日期,无需使用strtotime()

For full year you need to use this format Ymd . 全年你需要使用这种格式Ymd ymd will return you the date 16-03-06 but Ymd will return you 2016-03-06 . ymd将返回日期16-03-06 ,但Ymd将返回2016-03-06

Use a for loop with strtotime( "... days ago" ) : 使用带有strtotime( "... days ago" )for循环strtotime( "... days ago" )

for( $i = 0; $i < 4; $i++ )
{
    echo date( 'Y-m-d', strtotime( "$i days ago" ) ) . PHP_EOL;
}

The first loop ( 0 days ago ) will output today date, other loops will output past days. 第一个循环( 0 days ago )将输出今天的日期,其他循环将输出过去几天。

eval.in demo eval.in演示

<?php

$date = [date("Y-m-d")];
for($i = 1; $i < 4; $i++) {
    $date[] = date("Y-m-d",strtotime("-$i day"));
}
//For cli output you'll need:
echo implode("\n", $date) . "\n";
//For web output you'll need:
echo implode("<br />", $date) . "<br />";

You need to use capital 'Y' for full year (2016) instead of small 'y' which will display year in shorthand (16). 您需要使用资本'Y'作为全年(2016)而不是小'y',它将以速记(16)显示年份。

And for current date just use date("Ymd") . 而对于当前日期,只需使用date("Ymd")

<?php

$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));

echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;

?>

Working Example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM