SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30

Thread: PHP DateTime()

  1. #26
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    735
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    I see your point, so my basis is flawed to. PHP is currently subtracting the number of days in the preceding month, thus the March 31st minus a month is March 2nd, which is still wrong... So obviously it is a bit more complex than that too.

    This is why I agree with .NET:
    Code c#:
    var march = new DateTime(2012, 03, 31);
    var may = new DateTime(2012, 05, 31);
    Console.WriteLine(may.ToString("yyyy-MM-dd"));
    var april = may.AddMonths(-1);
    Console.WriteLine(april.ToString("yyyy-MM-dd"));
     
    var march = new DateTime(2012, 03, 31);
    Console.WriteLine(march.ToString("yyyy-MM-dd"));
    var february = march.AddMonths(-1);
    Console.WriteLine(february.ToString("yyyy-MM-dd"));
    var january = february.AddMonths(-1);
    Console.WriteLine(january.ToString("yyyy-MM-dd"));
    Console.ReadKey();
    /*
      Output:
      2012-05-31
      2012-04-30
      2012-03-31
      2012-02-29
      2012-01-29 // this one irks me a bit, but it is understandable.
    */

    I have to give it to the MS folks, they really made it nice and maybe I just expect other languages to do it as well.
    It's a little interesting because, based on results you've posted, .NET behavior will match common sense but break mathematics. For example:

    2012-03-31 -1 month returns 2012-02-29, and 2012-02-29 +1 month returns 2012-03-29. In other words, x + 1 -1 != x.
    "Folks who know what they're doing make complexity seem simple."

  2. #27
    Hosting Advisor silver trophybronze trophy
    SitePoint Award Recipient cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    2,806
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    It's a little interesting because, based on results you've posted, .NET behavior will match common sense but break mathematics. For example:

    2012-03-31 -1 month returns 2012-02-29, and 2012-02-29 +1 month returns 2012-03-29. In other words, x + 1 -1 != x.
    Yeah, I know. I had to run it three times to verify I didn't flub it up with some stupid mistake, but it does indeed do that. *sigh*, not as infallible as I was led to believe, but, I do appreciate the fact that like PHP, it does give you tools to get better validity when you need it (such as getting the last day in a given month consistently).

  3. #28
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,535
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Stupid calendar makers.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #29
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    1,863
    Mentioned
    28 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Off Topic:

    Stupid calendar makers.
    Fer realz.

    What I'm doing is running a daily batch of usage per billing cycle. A customer's billing cycle can land on any day (1-31). Daily I need to calculate their usage for their bill cycle. So today, the 25th, I'd be calculating for everyone whos bill cycle is on the 25th (Jan - Dec). On the 28th, I will run 4 seperate batches for the 28th, 29th, 30th and 31st to handle months that do not have certain days. This is why I need to be able to run on fictional dates..

    I'm going to use literal strings in this function to avoid any unwanted side effects, and handle the year by hand. I'll post the code when I have completed.
    <?php
    //Kyle Wolfe
    echo devBlog("My Dev Notes");

  5. #30
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    1,863
    Mentioned
    28 Post(s)
    Tagged
    0 Thread(s)
    Ugly but whatever:

    Code PHP:
    //bill cycle date to run for
    $day = 25;
    $month = 1;
    $year = 2013;
    if($month == 1) {
        $startDate = ($year-1) . '-12-' . $day;
        $endDate = $year . '-' . $month . '-' . $day;
    } else {
        $startDate = $year . '-' . ($month-1) . '-' . $day;
        $endDate = $year . '-' . $month . '-' . $day;
    }
    <?php
    //Kyle Wolfe
    echo devBlog("My Dev Notes");

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
  •