Jump to content

Determining day of week then subtract 1 day


chadrt
Go to solution Solved by Barand,

Recommended Posts

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 by chadrt
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 by chadrt
Link to comment
Share on other sites

  • 3 weeks later...

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

Link to comment
Share on other sites

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;

?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.