Jump to content

[SOLVED] Auto changing link depending on date


needshelp

Recommended Posts

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
Share on other sites

<?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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

  • 2 weeks later...
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.