Log in

View Full Version : Showing Sunday's date when it's not Sunday



lindsaycb
06-29-2007, 07:47 PM
So I have the code ready for use when it's Sunday to make a link with that day's date in it. That was easy. However, I would like to have the same date in the link always be until the following Sunday. So that if it's Tuesday, it will still show the previous Sunday's date in the link.

I thought this would do it
$lastWeek = mktime(0, 0, 0, date("m"), date("d")-7, date("Y"));
but that instead just take 7 days away from the current date; which makes sense once I saw what it was doing.

Any help on this problem? I solved my other problem with date and time using PHP, so I thought I could get this but it has me stumped. I don't know where else to go.

Thanks!

Lindsay

alexjewell
06-30-2007, 12:33 AM
I didn't make a code for this, but you could use a statement to see if it's been 7 days since the last Sunday, and if so, just make sure it's Sunday again (always will be), and then change the date to that Sunday and start it all over again...

djr33
06-30-2007, 02:35 AM
It's just some math.


$n = date('N');
if ($n == 7) { $n = 0; }
$lastWeek = mktime(0, 0, 0, date("m"), date("d")-$n, date("Y"));

lindsaycb
07-02-2007, 02:57 PM
And math has always been my weak point...

Maybe I'm a total newbie but don't you need an else if you're using an if?

And how can I then get the date plugged into a URL? I thought about making $lastWeek get plugged into a variable $Url but you can't quite do that... can you?

Here's what I came up with so far:


$n = date('N');
if ($n == 7) {
$n = 0;
} else {
$n = date('N');
}
$Sun = mktime(0, 0, 0, date("m"), date("d")-$n, date("Y"));
echo "Last Sunday was ".date("m/d/Y", $Sun);


However, that's just showing today's date... so I guess I'm not doing the else statement right...


It's just some math.


$n = date('N');
if ($n == 7) { $n = 0; }
$lastWeek = mktime(0, 0, 0, date("m"), date("d")-$n, date("Y"));

djr33
07-02-2007, 07:17 PM
The code I gave you should work, I think.
date('N') returns a numerical value, 1 for monday, 2 for tuesday, ..., 7 for Sunday. In the case of sunday, we want the current day, so I changed that value, IF 7, THEN 0.

An if statement must precede an else statement, but no else is required.

if ($n==7) { $n=0; }
else {$n = $n;}
Since the else is the default anyway, the if will be bypassed and nothing more is needed.
Note that the braces surround the if, so it won't continue through the rest of the code.

Just add that last line--
echo "Last Sunday was ".date("m/d/Y", $Sun);
to my code, and I think it should work.
Use $lastWeek instead of $Sun, or vice versa.

lindsaycb
07-02-2007, 07:32 PM
Here's the page in which I have the code, http://www.daily-chronicle.com/crossword/. It's showing today's date. And I have it copied from your code as is. That's why I'm confused as it why it's working. It makes sense to me now that I understand it better. Maybe it's just not possible to do it?

alexjewell
07-02-2007, 07:45 PM
Post your PHP here.

djr33
07-02-2007, 07:48 PM
Maybe it's set to show for Monday if i'm off by one. Let's wait a day to check. If so, just need to subtract one.

lindsaycb
07-03-2007, 01:56 PM
Well, unfortunately, that's not the case. It shows today's date now. I made it echo what the variables are holding, $n is just N. So that could be what's making it go all haywire; it's not a number. http://www.daily-chronicle.com/crossword/. I don't know why it's not a number though.


Maybe it's set to show for Monday if i'm off by one. Let's wait a day to check. If so, just need to subtract one.

lindsaycb
07-03-2007, 02:40 PM
Okay, so I tried using $n = date('w'); instead of the 'N' and it worked. The host server must have an older version of PHP. So now the variable $n is holding a number; since Sunday was 0 and today is Tuesday, it shows 2.

However, when echoing this code: echo "Last Sunday was ".date("m/d/Y", $Sun); it shows up at 7/01/2007. So that's works!

But when using this code: echo "The variable $ Sun is ".$Sun; it's not working and that's how I'm going to plug in my URL is using the echo $Sun; in the URL.

So I just used the echo date("Ymd", $Sun); in the href and it works (currently!) hopefully it'll work next Sunday!

Thanks for your help!

djr33
07-03-2007, 07:38 PM
Ok, glad it's working.
If the N component of the date wasn't working, that would cause trouble.
I hadn't used it before (since it's not something you need very much, the day of the week as a number), but sounded like it would work. It may very well have to do with the version of PHP.