View Full Version : Advice please - an easier way perhaps?
Having got a working redirect according to date, I wonder if there's an easier - or better - way of coding it.
The page has to change at midnight every Wednesday, beginning on 16th June. Is there a way to loop the $date check (and alter the $target value accordingly) or should I just go with 52 else if statements?
I realise it would be better to have no hard coding in at all to avoid having to change it next year, but I can't think my way round it.
<?php
$date= date("md");
$target="index.html";
if ($date<="0616") {$target="0616.html";}
else if ($date>"0616" && $date<="0623") {$target="0623.html";}
else if ($date>"0623" && $date<="0630") {$target="0630.html";}
else{}
Header("Location: $target");
echo " ";
?>
Have just discovered Pig's very useful article on date manipulation (but pleeeease ditch the orange text in the background which makes it very difficult to read the orange text in the foreground!) so hopefully will be able to resolve this myself.
I'll still accept guidance though! :)
This should work. The key is using the modulus of the daysinyear/7 .
<?php
#change the value of x to adjust the date if needed;
$x = 0;
$modulus_date = fmod(date(z), 7);
$cutoff_date = date(z) - $modulus_date + $x;
$weekly_date = date("md", mktime(0,0,0,1 ,$cutoff_date ,date(Y)));
echo '<a href="' . $weekly_date . '.html">' . $weekly_date . '.html</a><br/>';
?>
Thanks for that Louis - I'll have a play with that. At the moment, I have the following which worked quite nicely with test values. As this is the first script I've genuinely created from scratch by myself since taking the plunge in to php (as opposed to fiddling about with someone else's work), I'm childishly pleased with myself!
<?php
$now=time();
$start=strtotime("17 Jun 2004"); //1087430400 - date first page change is due
if ($now<$start)
{
$target=$start.".html";
}
else{
$end=$start+"604800";//604800=no of seconds in 7 days
$c=0;
do
{
if ($now>$start && $now<$end)
{
$target=$start.".html";
$c=0;
}
else
{
$start=$start+"604800";
$end=$start+"604800";
$c++;
}
}
while ($c>0);
}
Header("Location: $target");//remember to name pages with $start values
echo " "; // IF THIS ISN'T HERE NETSCAPE4 STALLS - but does anybody still use that??
?>
So all you want to do is change the url it is directed to, every week? You guys make life too hard. Coding is like making love. You must be graceful.
header("Location:" . strtotime( date("Y/m/" . (date("d") -date("w"))+3 ) ) );
Scoobie snack if you can tell me why this works. (assuming I understand your question)
I can see the rest of my day disappearing while I try to figure that one out! It certainly looks elegant! I'll be back later...
Nope. Can't see it. All I can see is that you will produce the strtotime for a day that is always 3 days ahead of the day it is when we arrive at the code.
My scenario is this:
From 16 Jun 2004, a comedy club takes place each Wednesday night, with 4 comedians appearing on stage. The website presently shows the details for the opening night (www.fatcatcomedy.com) so anybody accessing the site between now and then is to see this.
There will be different comedians appearing each week and my client wants the programme for Wednesday 23 Jun to be viewable from 17 Jun-23 Jun; that for Wednesday 30 Jun to be available from 24-30 Jun and so on.
So for each period of 7 days from midnight Wednesday, the same page is to display.
The promotional blurb for each act is already digitally available, and my client will advise me who is appearing each week. It's only a 5 minute job to cut and paste the info into the template and upload it. However, I don't want to be doing that every Wednesday night at midnight!! Hence my desire for a redirect according to date.
If yours does that Pig, would you be so kind as to explain it to me, because I really can't figure it out.
BTW I've enjoyed - and found very useful - your tutorials, and thanks for removing the background in the main "frame".
Yeah, I had meant to fix that for quite some time, but have too much going on. I just slapped a div around it and set the background color to white. In the future I might go for a fancier semi transparent background, but I'm not in a hurry.
Regarding your script though, the trick is to get wednesday at midnight for the current week. This is handled here:
(date("d") -date("w"))+3 )
date("d") gets the number of the month (for instance, 17th).
date("w") gets the number of the day (sunday=0, monday=1, tuesday=2, etc).
By subtracting 'w' from 'd' we force our day number to always be sunday. No matter what day we put in, we will always get Sunday.
We add 3 days to get Wednesday, and then feed into date() the current year, month and the newly calculated day.
Now that I disect this, I think it might need to be +10 instead of +3 in order to get the right week. Lemme play for a moment then I will post back.
Thinking about it, shouldn't it be -4? (After we've passed the start date). That way, whenever the redirect is accessed, it will display the current page which came into force at midnight on the preceding Wednesday (assuming I name the pages with the starting time).
OK, I'm back. Had to leave for a bit. Anyway, the problem is that our pivotal day is wednesday, not sunday. That means if they come in after sunday, it will take them back to sunday of that week, not to the next sunday. the number of days offset is variable, and not static. soooooo...
date('w') > 3 ? ($offset = 10) : ($offset = 3);
header("Location:" . strtotime( date("Y/m/" . (date("d") -date("w"))+$offset ) ) );
In case you haven't seen the shorthand if/else, it's the same as writing:
if( date('w') > 3 )
{
$offset = 10;
}
else
{
$offset = 3;
}
OK, I understand the processes (and no, I hadn't seen the shorthand, so thanks for that) but it still looks to me as if I'll be pointing to the page for the following week, not the current one.
Thanks for all your advice - I'll have a play around with it. :)
Maybe I'm confused. You want the page to display the page for the past week? If so, then just play around with the offset values.
Apologies if I wasn't clear enough.
A page becomes valid at midnight on Wednesday(date1) and gives details of the acts on the following Wednesday(date2), so any anybody visiting during that week should see the page from date1.
I've just discovered that there are problems with this at the beginning of the month, when the "day" can become a negative figure - and I guess that there will be occasions at the end of the month when the "day" could be 32 or 33!
Using this
strtotime(date("Y/m/d"))-(date("w")*86400)
gets around that and will always set the starting point as the previous Sunday before calculating any offset.
vBulletin v3.6.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.