Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Showing Sunday's date when it's not Sunday

  1. #1
    Join Date
    Jun 2007
    Location
    DeKalb, IL
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Showing Sunday's date when it's not Sunday

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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...
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's just some math.

    PHP Code:
    $n date('N');
    if (
    $n == 7) { $n 0; }
    $lastWeek mktime(000date("m"), date("d")-$ndate("Y")); 
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jun 2007
    Location
    DeKalb, IL
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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:
    PHP Code:
    $n date('N');
    if (
    $n == 7) {
      
    $n 0;
    } else {
      
    $n date('N');
    }
    $Sun mktime(000date("m"), date("d")-$ndate("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...

    Quote Originally Posted by djr33 View Post
    It's just some math.

    PHP Code:
    $n date('N');
    if (
    $n == 7) { $n 0; }
    $lastWeek mktime(000date("m"), date("d")-$ndate("Y")); 
    Last edited by lindsaycb; 07-02-2007 at 05:17 PM.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2007
    Location
    DeKalb, IL
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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?

  7. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Post your PHP here.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jun 2007
    Location
    DeKalb, IL
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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.

    Quote Originally Posted by djr33 View Post
    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.

  10. #10
    Join Date
    Jun 2007
    Location
    DeKalb, IL
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •