How can I find out the date of the day before a date?
Asked Answered
Z

9

7

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!

Zaller answered 23/4, 2009 at 14:16 Comment(0)
M
28

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);
Margarethe answered 23/4, 2009 at 14:18 Comment(0)
L
29

You can also use strtotime function using words like this:

$date = '2012-11-08';
$day_before = date( 'Y-m-d', strtotime( $date . ' -1 day' ) );

The output of $day_before:

2012-11-07
Longdistance answered 8/11, 2012 at 13:43 Comment(1)
I find this one more intuitive than the other solutions.Skirmish
M
28

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);
Margarethe answered 23/4, 2009 at 14:18 Comment(0)
A
6

You can rely on mktime()’s ability to work with “incorrect” values. Let’s assume $year, $month and $day are current 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.

Adnopoz answered 23/4, 2009 at 14:28 Comment(1)
+1 Didn't know about this mktime's ability... probably a better solution than mine.Margarethe
T
4

I would use strtotime indeed, in this way:

$date="2014-02-20";
echo date("Y-m-d",strtotime($date." -1 day"));

will output:

2014-02-19
Terrilyn answered 20/2, 2014 at 21:47 Comment(0)
A
2
$date = date('Y-m-d',strtotime('yesterday'));
Antagonize answered 8/4, 2011 at 13:56 Comment(2)
I was referring to the date before a specific date, not the date before todays date.Zaller
$date = date('Y-m-d',strtotime('yesterday',strtotime('2011-04-07')));Antagonize
C
2

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:

$yesterday = date('Y-m-d', strtotime($current_date) - 86400);

$current_date is the date you want to find the day before. If it is already a unix timestamp, you can dispense with strtotime() entirely:

$yesterday = date('Y-m-d', $current_date_as_unix_timestamp - 86400);
Comose answered 23/12, 2011 at 18:42 Comment(1)
This is not true! Yesterday was an hour shorter (daylight savings), so my code fetched the day before yesterday, which was the date 24 hours from now. So DO NOT use this code, it will screw up on day a year!!Terrilyn
I
0

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.

Ingle answered 23/4, 2009 at 14:26 Comment(1)
Unfortunately my employers aren't the kind of people to give me complete specs before hand, they just want me to make it and if they want something different afterwards have me change it.Zaller
W
0

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
Waterbuck answered 13/11, 2012 at 13:53 Comment(0)
R
0

One day before time at fixed 07:59:59

$demo = date("Y-m-d H:i:s", mktime(7, 59, 59, date("m"),date("d")-1,date("Y")));    
Roby answered 25/11, 2014 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.