如何找出日期前一天的日期? [英] How can I find out the date of the day before a date?

查看:135
本文介绍了如何找出日期前一天的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,可以将当前日期之前的会计年度与上一年的相同日期范围进行比较.您可以调整要查看的年份和月份,并且始终将相同的日期范围与上一年进行比较.我已经设置好了,因此,如果当前日期是leap日,则与去年的28日相比;如果去年是leap年,而今天是2月28日,则与去年相比,直到29日.如果您查看的是当前月份以外的月份,则显示的是该月的最后一天,否则显示的是当前日期.

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;


}

什么是最佳解决方案?

谢谢!

推荐答案

strtotime( )就能解决问题.使用 mktime()将您以前的日期转换为Unix时间戳,然后使用像这样:

strtotime() will do the trick. Convert your previous date to a Unix timestamp using mktime(), then use it like this:

$from_unix_time = mktime(0, 0, 0, $month, $day, $year);
$day_before = strtotime("yesterday", $from_unix_time);
$formatted = date('Y-m-d', $day_before);

这篇关于如何找出日期前一天的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆