php 我怎样才能找到约会前一天的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/781949/
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
How can I find out the date of the day before a date?
提问by JD Isaacks
I have a system that compares fiscal year up to the current date to the same date range of the year before. You can adjust the year and month to look at and it always compares the same date range to the year previous. I have it set so if the current day is leap day it compares to the 28th of last year, and if last year was leap year and today is feb 28th it compares to last year up to 29th. if you look at a month other than the current month it shows up to the last day of that month, otherwise up to current date.
我有一个系统将截至当前日期的财政年度与前一年的相同日期范围进行比较。您可以调整要查看的年份和月份,它始终将相同的日期范围与上一年进行比较。我已经设置好了,如果当前日期是闰日,则与去年的 28 日相比,如果去年是闰年,而今天是 2 月 28 日,则与去年的 29 日相比。如果您查看当前月份以外的月份,则显示到该月的最后一天,否则显示为当前日期。
OK that works fine right now, but now my employers don't want it to be up to the current date they want it to go up to yesterdays date. How can I do that, my main concerns are what if today is the 1st of the month, or what if today is the first day of the fiscal year.
好的,现在工作正常,但现在我的雇主不希望它更新到当前日期,他们希望它更新到昨天的日期。我该怎么做,我主要关心的是如果今天是一个月的第一天,或者今天是财政年度的第一天怎么办。
Here is the code I have now:
这是我现在拥有的代码:
function create_YTD_XML()
{
global $month;
global $year;
$last_year = $year - 1;
if($year == date('Y') && $month == date('m'))
{
$this_day = date('d');
}
else
{
$this_day = date('t', mktime(0, 0, 0, $month, 1, $year)); // LAST DAY OF MONTH
}
if(is_leap_year($year) && $this_day == 29)
{
$last_day = 28;
}
else if(is_leap_year($last_year) && $this_day == 28)
{
$last_day = 29;
}
else
{
$last_day = $this_day;
}
if($month >= 2)
{
$this_year_start = $year;
$last_year_start = $last_year;
}
else
{
$this_year_start = $year - 1;
$last_year_start = $last_year - 1;
}
$this_ytd_start = $this_year_start.'-02-01';
$last_ytd_start = $last_year_start.'-02-01';
$this_ytd_end = $year.'-'.str_pad($month, 2, "0", STR_PAD_LEFT).'-'.$this_day;
$last_ytd_end = $last_year.'-'.str_pad($month, 2, "0", STR_PAD_LEFT).'-'.$last_day;
}
What would be the best solution?
最好的解决方案是什么?
Thanks!
谢谢!
回答by Seb
strtotime()will do the trick. Convert your previous date to a Unix timestamp using mktime(), then use it like this:
strtotime()可以解决问题。使用mktime()将上一个日期转换为 Unix 时间戳,然后像这样使用它:
$from_unix_time = mktime(0, 0, 0, $month, $day, $year);
$day_before = strtotime("yesterday", $from_unix_time);
$formatted = date('Y-m-d', $day_before);
回答by Peon
回答by Ilya Birman
You can rely on mktime()'s ability to work with “incorrect” values. Let's assume $year, $month and $day are current day.
您可以依靠 mktime() 处理“不正确”值的能力。假设 $year、$month 和 $day 是当前日期。
$yesterday = mktime (0, 0, 0, $month, $day - 1, $year);
echo date ('Y-m-d', $yesterday);
Don't worry, it will just do the right thing: try yourself with different values.
别担心,它只会做正确的事情:尝试不同的价值观。
回答by JD Isaacks
Note:The following answer does not take daylight savings time into account; during certain times of the year, it will return an inaccurate result. See the comments for more information.
注意:以下答案没有考虑夏令时;在一年中的某些时候,它会返回不准确的结果。有关更多信息,请参阅评论。
Since the length of a day is constant (86,400 seconds), you can use arithmetic to determine the timestamp of the day before a given date:
由于一天的长度是恒定的(86,400 秒),您可以使用算术来确定给定日期之前一天的时间戳:
$yesterday = date('Y-m-d', strtotime($current_date) - 86400);
$current_dateis the date you want to find the day before. If it is already a unix timestamp, you can dispense with strtotime()entirely:
$current_date是您要查找前一天的日期。如果它已经是一个unix timestamp,你可以strtotime()完全省去:
$yesterday = date('Y-m-d', $current_date_as_unix_timestamp - 86400);
回答by patrick
I would use strtotime indeed, in this way:
我确实会以这种方式使用 strtotime:
$date="2014-02-20";
echo date("Y-m-d",strtotime($date." -1 day"));
will output:
将输出:
2014-02-19
回答by Stuff
$date = date('Y-m-d',strtotime('yesterday'));
回答by inxilpro
I think it depends on the behavior your employer wants. There's no "right" answer—if today is the start of a fiscal year, do they want to see the last two fiscal years compared (which is what would happen if you used yesterday as the "up to" date) or do they want to see day 1 of this fiscal year compared to day 1 of last? You need to sit down and figure out the potential edge cases and then talk with your employer about what they want.
我认为这取决于您的雇主想要的行为。没有“正确”的答案——如果今天是一个财政年度的开始,他们是希望看到过去两个财政年度的比较(如果你使用昨天作为“最新”日期会发生什么)还是他们想要与去年的第 1 天相比,查看本财政年度的第 1 天?您需要坐下来找出潜在的边缘情况,然后与您的雇主讨论他们想要什么。
回答by inxilpro
You can also use following code its works:
您还可以使用以下代码进行工作:
$current_date = strtotime('25-11-2012');
echo $yesterday = date('Y-m-d', strtotime('-1 day', $current_date));
And output of code is following:
代码输出如下:
2012-11-24
回答by Kamlesh
One day before time at fixed 07:59:59
前一天固定时间 07:59:59
$demo = date("Y-m-d H:i:s", mktime(7, 59, 59, date("m"),date("d")-1,date("Y")));

