Link to home
Create AccountLog in
Avatar of tjyoung
tjyoung

asked on

Date comparison to same 'wednesday' of previous year

Hi,
Been struggling with dates... again. And this could be a stupid question, so bear with me.
Today's issue is trying to compare the sales we did yesterday, (in my example lets say yesterday was Wednesday March 29, 2017), to one year ago (but the same wednesday, not simply 365 days ago as that would give me the wrong week day). In 2016 the Wednesday was March 30, 2016

I'm getting various dates for comparison like below:
$yesterday = Carbon::yesterday()->format('Y-m-d');
lastyear = Carbon::yesterday()->subMonths(12)->addDays(1)->format('Y-m-d');

Open in new window


This returns me the correct days for comparison. My question is... will this always work? Adding one day?
ASKER CERTIFIED SOLUTION
Avatar of JesterToo
JesterToo
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of tjyoung
tjyoung

ASKER

I get it. Yes that would be the way I'll do it. Just wondered if there was something I may be missing/function somewhere.
Thanks for the direction
Going forward, you might want to think about leaving these questions open a little longer.  There is a perfectly suitable solution to the one-year-ago problem.  But that said, day-by-day sales reports are almost always too granular to be useful.  What if it snowed on one of the days but was sunny and nice on the other?  The potential for wide and uncontrolled short-term variability make month-over-month and quarter-over-quarter statistics, generally, more meaningful.

Bookmark these articles for future reference.

Procedural
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html

OOP
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

See if this little function works the way you want.
<?php // demo/temp_tjyoung.php
/**
 * https://www.experts-exchange.com/questions/29012870/Date-comparison-to-same-'wednesday'-of-previous-year.html
 */
error_reporting(E_ALL);
echo '<pre>';


function yearAgoWeekDay($today = 'Today', $format = 'Y-m-d D')
{
    $day = date('l', strtotime($today));
    $ago = date('Y-m-d', strtotime("$today - 1 YEAR - 3 DAYS"));
    $ago = date($format, strtotime("$ago NEXT $day"));

    return $ago;
}


// TEST THE FUNCTION
$arr = date_range('December 1, 2015', 'March 15, 2017', 'Y-m-d D');
foreach ($arr as $my_date)
{
    echo PHP_EOL . $my_date . " Year Ago Weekday: " . yearAgoWeekDay($my_date);
}


// UNRELATED FUNCTION TO GET A range() ARRAY OF DATES
function date_range($a, $z, $format='Y-m-d')
{
    $return = [];
    $interval_p1d = new DateInterval('P1D');

    // NORMALIZE THE DATES (MAYBE USE TRY/CATCH SANITY CHECKS?)
    $alpha   = new DateTime($a);
    $omega   = new DateTime($z);

    // CREATE THE RANGE
    while ($alpha <= $omega) {
        $return[] = $alpha->format($format);
        $alpha->add($interval_p1d);
    }
    return $return;
}

Open in new window