如何找出约会前一天的日期?
我有一个系统,可以将截至当前日期的会计年度与上一年的同一日期范围进行比较。 您可以调整要查看的年份和月份,它总是将相同的日期范围与前一年进行比较。 我已经设置了,如果当天是闰日,则与去年的 28 日进行比较,如果去年是闰年,今天是 2 月 28 日,则与去年截至 29 日进行比较。 如果您查看当前月份以外的月份,它会显示到该月的最后一天,否则会显示到当前日期。
好吧,现在工作得很好,但现在我的雇主不希望它更新到当前日期,他们希望它更新到昨天的日期。 我该怎么做,我主要关心的是如果今天是这个月的第一天怎么办,或者如果今天是财政年度的第一天怎么办。
这是我现在拥有的代码:
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;
}
最好的解决方案是什么?
谢谢!
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
strtotime() 就可以了。 使用 mktime() 将之前的日期转换为 Unix 时间戳,然后像这样使用它:
strtotime() will do the trick. Convert your previous date to a Unix timestamp using mktime(), then use it like this:
您还可以使用strtotime函数使用单词,如下所示:
$day_before
的输出:You can also use strtotime function using words like this:
The output of
$day_before
:您可以依靠 mktime() 处理“不正确”值的能力。 假设 $year、$month 和 $day 是当天。
别担心,它只会做正确的事情:尝试不同的价值观。
You can rely on mktime()’s ability to work with “incorrect” values. Let’s assume $year, $month and $day are current day.
Don’t worry, it will just do the right thing: try yourself with different values.
我确实会以这种方式使用 strtotime:
将输出:
I would use strtotime indeed, in this way:
will output:
注意:以下答案不考虑夏令时; 在一年中的某些时候,它会返回不准确的结果。 请参阅评论以获取更多信息。
由于一天的长度是恒定的(86,400 秒),因此您可以使用算术来确定给定日期之前一天的时间戳:
$current_date
是您要查找前一天的日期。 如果它已经是 unix 时间戳,则可以省去strtotime()
完全: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:
$current_date
is the date you want to find the day before. If it is already a unix timestamp, you can dispense withstrtotime()
entirely:我认为这取决于你的雇主想要的行为。 没有“正确”的答案 - 如果今天是一个财政年度的开始,他们是否想要查看过去两个财政年度的比较(如果您使用昨天作为“最新”日期,就会发生这种情况)或者他们想要查看本财年第一天与上一财年第一天的比较? 您需要坐下来找出潜在的边缘情况,然后与您的雇主讨论他们想要什么。
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.
您还可以使用以下代码其工作原理:
并且代码的输出如下:
You can also use following code its works:
And output of code is following:
一天前固定时间 07:59:59
One day before time at fixed 07:59:59