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?
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
JesterToo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
Ray Paseur

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes