SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Generating a 'Y-m-d' value for the start of the previous week

    Hi guys!

    I have some code that calculates the date for the beginning of the previous week.

    PHP Code:
    date("Y-m-d"time() - (date('w') - 1) * (60 60 24)) 
    However, in MySQL, the date needs to be shifted back one day to grab the first day of the date.

    Now, I have actually solved this problem.

    PHP Code:
    $array_date explode('-'date("Y-m-d"time() - (date('w') - 1) * (60 60 24)));
    $array_search['start'] = date('Y-m-d'mktime(000$array_date[1], $array_date[2] - 1$array_date[0])); 
    But it's a kludge.

    So this is more out of curiosity than anything else, I'd just like to know what would need to be done to the first code sample to add on that extra day at the beginning.

    I've tried everything I can think of, so any solution would be much appreciated!

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    The second kludge, as you call it, almost made my head ache! I think you should simplify that code ASAP

    Quote Originally Posted by Forbes View Post
    I have some code that calculates the date for the beginning of the previous week.

    PHP Code:
    date("Y-m-d"time() - (date('w') - 1) * (60 60 24)) 
    This doesn't seem to work as expected, today it's sunday 2011-10-16 and the code above returns 2011-10-17, which is monday tomorrow and not monday last week! It's because date('w')-1 equals -1, which means you are subtracting a negative number, which works like adding a positive number. These are pitfalls of using complicated code, I think using the DateTime object will make it much easier for you.

    Do you consider Monday or Sunday to be the beginning of the week? I'll assume you want Monday, so here you have the code to grab the beginning of last (previous) week plus the day before:
    PHP Code:
    $date = new DateTime('last Monday');
    if (
    date('D') != 'Mon') {
        
    // if today is not Monday, then go one week earlier
        
    $date->modify('-1 week');
    }
    $prev_week_start $date->format('Y-m-d');

    $date->modify('-1 day');
    $day_before $date->format('Y-m-d'); 
    Works in PHP 5.2+.

  3. #3
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I realised that the two dates would be the same at the beginning of the week, which is why I subtracted one day from the date for the starting date of the previous week. But that's not what I'm asking about; I want a more elegant way of calculating the starting date of the previous week that includes a subtraction of one day.

    I'll have a play around with the date object and see if that simplifies things. As an aside, I'll probably be spinning the code off as a method in a helper class.

    Thanks for the feedback.

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Forbes View Post
    Hi, I realised that the two dates would be the same at the beginning of the week, which is why I subtracted one day from the date for the starting date of the previous week. But that's not what I'm asking about;
    Okay, I just commented on that code because when run on a Sunday, it adds one day instead of subtracting, which is why it returns the date for tomorrow. Because in that case it doesn't do what you said it does I decided to commend on that as well
    I want a more elegant way of calculating the starting date of the previous week that includes a subtraction of one day.
    Isn't that what I included in the last 2 lines of my sample code? Or maybe I don't fully understand what you are looking for?

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    date('Y-m-d',strtotime('previous week monday'));

    http://www.php.net/manual/en/datetim...s.relative.php
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    date('Y-m-d',strtotime('previous week monday'));
    This will work only on Mondays. If it's Tuesday it will return date for Monday yesterday, the OP wanted Monday of the previous week. It's illogical but here 'previous week monday' means simply 'last Monday'. The date/time parser seems to lack intelligence in this case.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    This will work only on Mondays. If it's Tuesday it will return date for Monday yesterday, the OP wanted Monday of the previous week. It's illogical but here 'previous week monday' means simply 'last Monday'. The date/time parser seems to lack intelligence in this case.
    False.
    I just changed my system clock to Tuesday, the 18th, and ran this:
    PHP Code:
    <?php
    echo date('Y-m-d',strtotime('previous week monday'))."<br>";
    echo 
    date('Y-m-d');
    ?>
    And got out:
    2011-10-10
    2011-10-18

    So... it DOES have intelligence.

    If i had said "previous monday", it would have returned the 17th; but throwing Week in changes the statement.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    False.
    I just changed my system clock to Tuesday, the 18th, and ran this:
    PHP Code:
    <?php
    echo date('Y-m-d',strtotime('previous week monday'))."<br>";
    echo 
    date('Y-m-d');
    ?>
    And got out:
    2011-10-10
    2011-10-18
    That's interesting because I could say false to your false! On my computer when I set the clock to the 18th I get this:

    2011-10-17
    2011-10-18

    This is on Windows, PHP 5.2.17. What version are you using? Maybe it's a bug that has been fixed.

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    XAMPP, Windows, PHP v5.3.5. So perhaps the 5.3 branch has more logic in it.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  10. #10
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    So 5.3 must be more logical. Still, I would advise to test this for all days of the week. "previous week" may mean something different depending on which day the parser considers to be the beginning of the week: it could be Monday or Sunday. If it's Sunday the results can be unexpected. And if the day which begins the week depends on the system locale then this would make this code bad for portability - I'm just speculating, someone would need to confirm how the parser works. If I'm not sure I would prefer not to rely on some obscure intelligence deciding what 'previous week' means.

  11. #11
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, in light of the possible discrepancies (and this thread has proved very interesting), I'll keep the previous code and use the code by StarLion for now, pending any weirdness.

    Guys, thanks!


Tags for this Thread

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
  •