Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on 

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

Avatar of undefined
Last Comment
Ray Paseur
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Blurred text
THIS SOLUTION IS 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
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS 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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

stale question
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo