How would you fix this?

Here's the code as it exists currently:

$todaydatetime=date("Y-m-d");

$lastsyncdatetime=new DateTime();
$the_date=$lastsyncdatetime->format("Y-m-d");
$subtract_thirty=strtotime('-30 day', strtotime($the_date));
$subtract_thirty_calc=date("Y-m-d", $subtract_thirty);

$syncyear = date('Y',$subtract_thirty);
$syncmonth =date('m',$subtract_thirty);		
$syncday = date('d',$subtract_thirty);	

while ($todaydatetime > $subtract_thirty_calc) {

//do some database stuff

$syncday++;
$lastsyncdate = date("m/d/y",mktime(0,0,0,$syncmonth,$syncday,$syncyear));
$comptime = mktime(0,0,0,$syncmonth,$syncday,$syncyear);
$syncyear = date('Y',$comptime);
$syncmonth =date('m',$comptime);
$syncday = date('d',$comptime);	
//$lastsyncdatetime is now $subtract_thirty_calc and you've got to figure out how to increment that
$subtract_thirty_calc = new DateTime($syncyear.'-'.$syncmonth.'-'.$syncday);		

}

Open in new window


This isn't my code. My job is to clean it up and I've had a hard time up to this point just because there appeared to be several instances where they were using the DateTime object improperly. In other words, they were making comparisons and doing calculations on an object so when I went to test it, the system generated a warning saying that it needed a string.

That being the case, I went through and made some changes but I'm stuck. Right now it appears as though the above loop will advance the day, but, again, the very last line you've got $subtract_thirty_calc and it looks as though they're trying to increase the original date value by one day, but it's not working and I'm stuck as to how you would advance the day and not wind up with something like February 31st.

Bottom line: What do I need to do after $syncday++ to increment the $subtract_thirty_calc date and have it properly formatted so it can continue being used in the comparison that's happening at the top of the "while" loop?

Thanks!
brucegustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You probably want to look at the DateTime->add() function
Or the strtotime() function
0
Ray PaseurCommented:
making comparisons and doing calculations on an object
That may be quite possible.  Can you please show us one of the instances that illustrates what you're concerned about?
when I went to test it, the system generated a warning
Please show us how you tested it, thanks.

If you haven't already spent a little time with this article, it might be helpful.
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

This might do what you need, but please consider the difference between a month and 30 days.  If you need a month, you almost certainly want a different calculation because most months do not have 30 days.  The article covers this issue in one of the code examples.

Please see: https://iconoun.com/demo/temp_brucegust.php
<?php // demo/temp_brucegust.php
/**
 * https://www.experts-exchange.com/questions/29007369/How-would-you-fix-this.html
 *
 * http://php.net/manual/en/datetime.construct.php
 * http://php.net/manual/en/class.dateinterval.php
 * http://php.net/manual/en/dateinterval.construct.php
 */
error_reporting(E_ALL);

$current  = new DateTime();
$future   = new DateTime('Today + 30 days');
$interval = new DateInterval('P1D');
while ($current < $future)
{
    echo PHP_EOL . $current->format('Y-m-d');
    $current->add($interval);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
stale question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.