needshelp Posted March 26, 2007 Share Posted March 26, 2007 I want to have a special link on my site. The link is to a text file located on a distributor's website and is named with a specific filename in the same format every week. In other words it's guaranteed predictable. Here is what the file would look like, shipping_032107.txt which means, shipping_(month)(day)(year).txt How could I use something to make the link the right name without modifying my page weekly? The one tricky part is that it's updated once a week and it would be Wednesdays date each time. So it actually has to predict the next date. Perhaps if the script couldn't do that it could figure out todays date, check it to the days of the week and add a number to equal the proper day, I have no idea what to start with for this. Anyone have any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/ Share on other sites More sharing options...
AndyB Posted March 27, 2007 Share Posted March 27, 2007 <?php list($y,$m,$d) = explode("-",date("y-m-d")); // today $dotw = date(w); // day of the week $thiswed = $d+3 - $dotw; $prevwed = $thiswed - 7; $nextwed = $thiswed +7; // what is the date for the NEXT wednesday $nextwed = date("y-m-d", mktime(0,0,0,$m,$nextwed,$y)); list($ny,$nm,$nd) = explode("-",$nextwed); $linkfile1 = "shipping_". $nm. $nd. $ny. ".txt"; // what is the date for the THIS wednesday $thiswed = date("y-m-d", mktime(0,0,0,$m,$thiswed,$y)); list($ty,$tm,$td) = explode("-",$thiswed); $linkfile2 = "shipping_". $tm. $td. $ty. ".txt"; // what is the date for the previous wednesday $prevwed = date("y-m-d", mktime(0,0,0,$m,$prevwed,$y)); list($py,$pm,$pd) = explode("-",$prevwed); $linkfile3 = "shipping_". $pm. $pd. $py. ".txt"; if (file_exists($linkfile1)) { echo "<a href='".$linkfile. "'>$linkfile</a>"; } else { if (file_exists($linkfile2)) { echo "<a href='".$linkfile2. "'>$linkfile2</a>"; } else { if (file_exists($linkfile3)) { echo "<a href='".$linkfile3. "'>$linkfile3</a>"; } } } ?> Which works to display "next Wednesday's" if it exists, otherwise "this Wednesday's" or "last wednesday's" depending on what day of the week tody is. Not the most elegant, but seems to work for me. Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-215792 Share on other sites More sharing options...
hitman6003 Posted March 27, 2007 Share Posted March 27, 2007 <?php //get variables for today's date list($year, $month, $day, $day_of_week) = explode("-", date("Y-m-d-w")); //determine if there is an offset, and if so, what it is //using the date('w') function the days are 0-6, so we can //change which day we want by changing the 3's below: //-------------------------\/--------------------\/------- $offset = ($day_of_week != 3) ? ($day_of_week - 3) + 7 : 0; //use the offset to get the datestring for last Wednesday echo "filename: shipping_" . date("mdy", mktime(null, null, null, $month, $day - $offset, $year)); ?> Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-215794 Share on other sites More sharing options...
hitman6003 Posted March 27, 2007 Share Posted March 27, 2007 Or, if you want to functionalize it: function find_day_of_prev_week($year, $month, $day, $day_of_week) { /* $year, $month, and $day should be integers, obviously valid date ranges. $day_of_week should be an int from 0 to 6: 0 = Sunday, 6 = Saturday */ $day_of_this_week = date("w", mktime(null, null, null, $month, $day, $year)); //determine if there is an offset, and if so, what it is //this will not modify the date if today is the day we are looking for... //i.e. if we are looking for wednesday, and today is wednesday, it will return today. //that behavior can be changed by commenting the below and uncommenting it's successor $offset = ($day_of_this_week != $day_of_week) ? ($day_of_this_week - $day_of_week) + 7 : 0; //$offset = $day_of_this_week - $day_of_week + 7; //take note that this will return less than zero if the previous day is in a previous month... //it is intended to be passed to the mktime function return $day - $offset; } echo ' Today: ' . date("D M j G:i:s T Y") . '<br /> Last Today: ' . find_day_of_prev_week(date('Y'), date('m'), date('d'), date('w')) . '<br />'; $day_of_week_of_first = date('w', mktime(0, 0, 0, date("m"), 1, date("Y"))); $prev_day = find_day_of_prev_week(date('Y'), date('m'), 1, $day_of_week_of_first); $same_day_as_week_prev = date("D M j G:i:s T Y", mktime(0, 0, 0, date('m'), $prev_day, date('Y'))); echo' <br />The First: ' . date("D M j G:i:s T Y", mktime(0, 0, 0, date("m"), 1, date("Y"))) . '<br /> The previous d-o-w: ' . $prev_day . '<br /> Passed to mktime and date: ' . $same_day_as_week_prev; Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-215804 Share on other sites More sharing options...
needshelp Posted March 28, 2007 Author Share Posted March 28, 2007 I follow the code fairly well, although I'm as amateur as they come but one spot is troubling me. When I add in the web address and I'm sure I'm doing this wrong it gives me an error on the line. Using your code AndyB, I'm adding (for example) http://www.website.com/shipping/ to if (file_exists($linkfile1)) { echo "<a href='"http://www.website.com/shipping/.$linkfile1. "'>$linkfile1</a>"; } else { if (file_exists($linkfile2)) { echo "<a href='"http://www.website.com/shipping/.$linkfile2. "'>$linkfile2</a>"; } else { if (file_exists($linkfile3)) { echo "<a href='"http://www.website.com/shipping/.$linkfile3. "'>$linkfile3</a>"; } } } ?> I'm sure its something easy and obvious I should know but I can't seem get it at all. Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-216576 Share on other sites More sharing options...
dsaba Posted March 28, 2007 Share Posted March 28, 2007 if you're going to ask "why does it give me this error on this line?" you're going to need to post that error and identify which line it is erroring in aren't you? Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-216599 Share on other sites More sharing options...
AndyB Posted March 28, 2007 Share Posted March 28, 2007 echo "<a href='"http://www.website.com/shipping/.$linkfile1. "'>$linkfile1</a>"; watch those single and double quotes! It should be: echo "<a href='http://www.website.com/shipping/" .$linkfile1. "'>$linkfile1</a>"; Same for the other equivalent links. Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-216660 Share on other sites More sharing options...
needshelp Posted March 28, 2007 Author Share Posted March 28, 2007 Your right dsaba, but in this case the line of code thats wrong is in my brain, lol. That did the trick AndyB, its calling the script up perfectly. Although today is Wednesday, we will see how it goes tomorrow, haha. Can I add an image as the link? If so I could have a different image for each of the; next, this and previous lines. I can't get it to work with the normal image tags? Doesn't it work just like a normal image link? Thanks for the help everyone! and thank you AndyB <wink> Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-217106 Share on other sites More sharing options...
needshelp Posted April 2, 2007 Author Share Posted April 2, 2007 Any idea on the image question? I still can't get it to work. Thanks, Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-220169 Share on other sites More sharing options...
needshelp Posted April 8, 2007 Author Share Posted April 8, 2007 echo "<a href='http://www.website.com/shipping/" .$linkfile1. "'> /\ </a>"; | | | | | | This is where I want the image link. Please any help would be great, I can't seem to find this simple thing. It should be easier than my original question. I'm desperate! It's so close to being completed and working properly! Thanks! Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-223962 Share on other sites More sharing options...
dsaba Posted April 8, 2007 Share Posted April 8, 2007 <a href="$linkurl"><img src="$imageurl"></a> use php variables to produce your dynamic url for the image file, and also for the link url learn how to make images links here: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_imglink -good luck Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-223988 Share on other sites More sharing options...
needshelp Posted April 8, 2007 Author Share Posted April 8, 2007 Super! I'll check that out! Thanks!! Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-224468 Share on other sites More sharing options...
needshelp Posted April 16, 2007 Author Share Posted April 16, 2007 Once I realized what that last link actually was I felt kind of insulted... Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-230814 Share on other sites More sharing options...
AndyB Posted April 16, 2007 Share Posted April 16, 2007 echo "<a href='http://www.website.com/shipping/" .$linkfile1. "'><img src='images/wombat.gif' border='0'>[/a]"; That should point you to success. Just watch the " and ' quotes. Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-230829 Share on other sites More sharing options...
needshelp Posted April 17, 2007 Author Share Posted April 17, 2007 Bingo! Thanks very much. Once again the quotes get me! Thanks again! Link to comment https://forums.phpfreaks.com/topic/44419-solved-auto-changing-link-depending-on-date/#findComment-230864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.