chadrt Posted February 2, 2015 Share Posted February 2, 2015 (edited) I have a PHP script that downloads a file from a remote server but in order to download the correct file it has to subtract 1 day of the week. It used to work quite some time ago so I dont know what is going on now. The section of the script goes like this.... $days = array("sun","mon","tue","wed","thu","fri","sat"); $today = date("w"); $yesterday = $days[$today - 1]; Then it joins the 3 letter version of the day of week with a file name to grab the correct file download. $yesterday is not returning the correct value instead I am getting an error... # ./update.php PHP Notice: Undefined offset: -1 in /root/fcc/work/update.php on line 6 fetching l_am_.zip ncftpget: server said: l_am_.zip: No such file or directory. PHP Warning: unlink(counts): No such file or directory in /root/fcc/work/update.php on line 23 data set empty nothing to process Sun, 01 February, 2015 6:29:06 PM So in the above error I can see that $yesterday is "Undefined offset" and because of that the file it tries to download is incorrect, it should have tried to download l_am_sat.zip and it left out the 3 letter date code. Edited February 2, 2015 by chadrt Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/ Share on other sites More sharing options...
mac_gyver Posted February 2, 2015 Share Posted February 2, 2015 your code does not address underflow/wrap-a-round and will throw this error any time it is run on a Sunday. on Sunday, date('w') produces a zero. you would need to detect that and insure that your code ends up accessing $array[6] corresponding to Saturday. Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1504528 Share on other sites More sharing options...
chadrt Posted February 2, 2015 Author Share Posted February 2, 2015 I will solve that by leaving unexecuted on Sundays then. There is a separate script that runs every Sunday that wipes the database out and give it a brand new load from scratch, so that if there is a problem during the week the data will be fixed on Sundays. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1504531 Share on other sites More sharing options...
Solution Barand Posted February 2, 2015 Solution Share Posted February 2, 2015 $yesterday = strtolower((new DateTime('yesterday'))->format('D')); Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1504536 Share on other sites More sharing options...
chadrt Posted February 3, 2015 Author Share Posted February 3, 2015 (edited) My PHP skills are very limited so I end up hacking up scripts from other borrowing from one to make another. LOL This is how I accomplished the task that I had. $days = array("sun","mon","tue","wed","thu","fri","sat"); $today = date("w"); if ($today==0){ $yesterday = "sat"; }else{ $yesterday = $days[$today -1]; if ($yesterday==0){ die("Script terminated no updates are performed on Sundays!\n"); } } My code is not ellegant by any means but I think it will work what do you all think? So I try to post my code these days as I think it should be rather than telling you all to do the coding for me! My construct above should solve for the problem, if for some reason you feel there is a more elegant way of doing this then I am all ears. I have another script that reloads the database from scratch that usually runs from a separate cron job. I see this script now the way I have formatted performing the work of the second script now too by integrating the code into the second "if" by removing the "die()" and replacing it with the scripting from the weekly reload script. Edited February 3, 2015 by chadrt Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1504630 Share on other sites More sharing options...
chadrt Posted February 3, 2015 Author Share Posted February 3, 2015 Barand, Thank you that was a very elegant solution! I actually had to take it apart and read up on everything you did in that line but I understand now. Thank you Chad Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1504759 Share on other sites More sharing options...
chadrt Posted February 22, 2015 Author Share Posted February 22, 2015 OK so today it is not working again when I run my script it says: PHP Parse error: syntax error, unexpected '$yesterday' (T_VARIABLE) in /root/fcc/update.php on line 17 I used Barand's solution from above: $yesterday = strtolower((new DateTime('yesterday'))->format('D')); What is really odd about the whole thing is that it was working and there hae been absolutely NO changes since it was working a couple weeks ago. How odd!! Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1506386 Share on other sites More sharing options...
chadrt Posted February 22, 2015 Author Share Posted February 22, 2015 I copied that string to a new file test.php... #!/usr/bin/php -q <?php # $yesterday = strtolower((new DateTime('yesterday'))->format('D')); //THIS WONT WORK echo strtolower((new DateTime('yesterday'))->format('D')); //BUT THIS DOES WORK! GRRRRRR... # echo $yesterday; ?> Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1506387 Share on other sites More sharing options...
Barand Posted February 22, 2015 Share Posted February 22, 2015 Have you added a new line 16 and forgotten the ; at the end of the line? Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1506396 Share on other sites More sharing options...
chadrt Posted February 23, 2015 Author Share Posted February 23, 2015 I coppied some information out of the file and one of the # was accidently deleted. Grrrrrrr... I searched that file from top to bottom I cant believe that I missed that!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/294305-determining-day-of-week-then-subtract-1-day/#findComment-1506491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.