How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lwoods

    How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

    I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
    than today.

    How do I do that?

    TIA,

    Larry Woods


  • Mladen Gogala

    #2
    Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

    On Fri, 13 Jan 2006 07:21:40 -0800, lwoods wrote:
    [color=blue]
    > I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
    > than today.
    >
    > How do I do that?
    >
    > TIA,
    >
    > Larry Woods[/color]


    PEAR Date package can subtract/add seconds.

    --
    大红鹰娱乐平台采用顶级加密技术,确保用户数据与交易的安全无虞,让玩家能够放心畅玩。


    Comment

    • lwoods

      #3
      Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

      Thanks, but I don't need to subtract seconds, really. I need something that
      looks like this:

      I need to be able to:

      1. Subtract 10 years from today
      2. Display that value in 'mm/dd/yyyy' format

      I guess I assumed that PHP had the capability of doing this without
      resorting to additional packages like PEAR

      Larry

      "Mladen Gogala" <gogala@sbcglob al.net> wrote in message
      news:pan.2006.0 1.13.14.35.40.6 38327@sbcglobal .net...[color=blue]
      > On Fri, 13 Jan 2006 07:21:40 -0800, lwoods wrote:
      >[color=green]
      >> I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
      >> than today.
      >>
      >> How do I do that?
      >>
      >> TIA,
      >>
      >> Larry Woods[/color]
      >
      >
      > PEAR Date package can subtract/add seconds.
      >
      > --
      > http://www.mgogala.com
      >[/color]


      Comment

      • Andrew @ Rockface

        #4
        Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlierthan today.

        lwoods wrote:[color=blue]
        > Thanks, but I don't need to subtract seconds, really. I need something that
        > looks like this:
        >
        > I need to be able to:
        >
        > 1. Subtract 10 years from today
        > 2. Display that value in 'mm/dd/yyyy' format[/color]

        $date = "01/14/2005";
        $date_new = date('m\/d\/Y', strtotime("10 years ago", strtotime($date )));
        echo $date_new;

        --
        Andrew @ Rockface
        np: Conflict - Vietnam [stopped]

        Comment

        • Mladen Gogala

          #5
          Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

          On Fri, 13 Jan 2006 07:48:34 -0800, lwoods wrote:
          [color=blue]
          > Thanks, but I don't need to subtract seconds, really. I need something that
          > looks like this:
          >
          > I need to be able to:
          >
          > 1. Subtract 10 years from today[/color]

          That only means that you have to extract enough seconds. Try with
          3650*24*3600

          [color=blue]
          > 2. Display that value in 'mm/dd/yyyy' format[/color]

          And the Date class has a way of doing that.

          --
          大红鹰娱乐平台采用顶级加密技术,确保用户数据与交易的安全无虞,让玩家能够放心畅玩。


          Comment

          • Kimmo Laine

            #6
            Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

            "lwoods" <larry@lwoods.c om> kirjoitti
            viestissä:ZGOxf .7893$JT.6131@f ed1read06...[color=blue]
            >I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
            >than today.
            >
            > How do I do that?[/color]


            $date = "12/31/2005";
            echo date('d/m/Y', strtotime("$dat e -10years"));

            --
            SETI @ Home - Donate your cpu's idle time to science.
            Further reading at <http://setiweb.ssl.ber keley.edu/>
            Kimmo Laine <antaatulla.sik anautaa@gmail.c om.NOSPAM.inval id>


            Comment

            • Pedro Graca

              #7
              Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

              lwoods wrote:[color=blue]
              > I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
              > than today.
              >
              > How do I do that?[/color]

              Lots of ways.
              Here's one

              echo date('m/d/Y', mktime(0, 0, 0, date('m'), date('d'), date('Y')-10));

              --
              Mail to my "From:" address is readable by all at http://www.dodgeit.com/
              == ** ## !! ------------------------------------------------ !! ## ** ==
              TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
              may bypass my spam filter. If it does, I may reply from another address!

              Comment

              • Chung Leong

                #8
                Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

                Take the current year and substract 10. It's not rocket science.

                $dzisiaj = getdate();
                echo sprintf("%02d/%02d/%d", $dzisiaj['mon'], $dzisiaj['mday'],
                $dzisiaj['year'] - 10);

                Comment

                • john.d.mann@sbcglobal.net

                  #9
                  Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlierthan today.

                  lwoods wrote:[color=blue]
                  > I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
                  > than today.
                  >
                  > How do I do that?
                  >
                  > TIA,
                  >
                  > Larry Woods[/color]

                  explode() it, perform the calculation, and then implode() it. That's
                  what I would do, anyhow.

                  // configuration
                  $date = '01/01/2006';
                  $offset = '-10';
                  $date_glue = '/';

                  // implementation
                  $temp = explode($date_g lue, $date);
                  $temp[2] = $temp[2] + $offset;
                  $date = implode ($date_glue, $date);
                  echo $date;

                  Comment

                  • VS

                    #10
                    Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlierthan today.

                    john.d.mann@sbc global.net wrote:[color=blue]
                    > lwoods wrote:
                    >[color=green]
                    >> I want to calculate and display the 'mm/dd/yyyy' that is 10 years
                    >> earlier than today.
                    >>
                    >> How do I do that?
                    >>
                    >> TIA,
                    >>
                    >> Larry Woods[/color]
                    >
                    >
                    > explode() it, perform the calculation, and then implode() it. That's
                    > what I would do, anyhow.
                    >
                    > // configuration
                    > $date = '01/01/2006';
                    > $offset = '-10';
                    > $date_glue = '/';
                    >
                    > // implementation
                    > $temp = explode($date_g lue, $date);
                    > $temp[2] = $temp[2] + $offset;
                    > $date = implode ($date_glue, $date);
                    > echo $date;[/color]

                    That technique only works for some date calculations.

                    Try to take off 10 days during the first 10 days of the month or 10
                    months between Jan and October and you'll get strange results.

                    --
                    VS

                    Comment

                    • john.d.mann@sbcglobal.net

                      #11
                      Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlierthan today.

                      VS wrote:[color=blue]
                      > john.d.mann@sbc global.net wrote:
                      >[color=green]
                      >> lwoods wrote:
                      >>[color=darkred]
                      >>> I want to calculate and display the 'mm/dd/yyyy' that is 10 years
                      >>> earlier than today.
                      >>>
                      >>> How do I do that?
                      >>>
                      >>> TIA,
                      >>>
                      >>> Larry Woods[/color]
                      >>
                      >>
                      >>
                      >> explode() it, perform the calculation, and then implode() it. That's
                      >> what I would do, anyhow.
                      >>
                      >> // configuration
                      >> $date = '01/01/2006';
                      >> $offset = '-10';
                      >> $date_glue = '/';
                      >>
                      >> // implementation
                      >> $temp = explode($date_g lue, $date);
                      >> $temp[2] = $temp[2] + $offset;
                      >> $date = implode ($date_glue, $date);
                      >> echo $date;[/color]
                      >
                      >
                      > That technique only works for some date calculations.
                      >
                      > Try to take off 10 days during the first 10 days of the month or 10
                      > months between Jan and October and you'll get strange results.
                      >[/color]

                      Well, sure, but he was only asking about the year hehe. For the day or
                      month, a more advanced version would be required.

                      John D. Mann

                      Comment

                      • Berimor

                        #12
                        Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

                        On Fri, 13 Jan 2006 15:26:11 GMT, Mladen Gogala <gogala@sbcglob al.net>
                        wrote:
                        [color=blue]
                        >
                        > That only means that you have to extract enough seconds. Try with
                        > 3650*24*3600
                        >[/color]

                        Really?
                        What about leap-years?






                        --
                        Exact Meta Search | Major Search Engine http://exactsearcher.com
                        Web Design Essex | Multimedia | Printing http://nextwave.co.uk

                        Comment

                        • Oli Filth

                          #13
                          Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlierthan today.

                          john.d.mann@sbc global.net said the following on 13/01/2006 18:34:[color=blue]
                          > VS wrote:
                          >[color=green]
                          >> john.d.mann@sbc global.net wrote:
                          >>[color=darkred]
                          >>> lwoods wrote:
                          >>>
                          >>>> I want to calculate and display the 'mm/dd/yyyy' that is 10 years
                          >>>> earlier than today.
                          >>>>
                          >>> // configuration
                          >>> $date = '01/01/2006';
                          >>> $offset = '-10';
                          >>> $date_glue = '/';
                          >>>
                          >>> // implementation
                          >>> $temp = explode($date_g lue, $date);
                          >>> $temp[2] = $temp[2] + $offset;
                          >>> $date = implode ($date_glue, $date);
                          >>> echo $date;[/color]
                          >>
                          >>
                          >>
                          >> That technique only works for some date calculations.
                          >>
                          >> Try to take off 10 days during the first 10 days of the month or 10
                          >> months between Jan and October and you'll get strange results.
                          >>[/color]
                          >
                          > Well, sure, but he was only asking about the year hehe. For the day or
                          > month, a more advanced version would be required.
                          >[/color]

                          How about 29/02/2004?


                          --
                          Oli

                          Comment

                          • Philip Ronan

                            #14
                            Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

                            Berimor wrote:
                            [color=blue]
                            > On Fri, 13 Jan 2006 15:26:11 GMT, Mladen Gogala <gogala@sbcglob al.net>
                            > wrote:
                            >[color=green]
                            >>
                            >> That only means that you have to extract enough seconds. Try with
                            >> 3650*24*3600
                            >>[/color]
                            >
                            > Really?
                            > What about leap-years?[/color]

                            And wasn't there a leap second at the beginning of this month?

                            --
                            philronan [@] blueyonder [dot] co [dot] uk

                            Comment

                            • john.d.mann@sbcglobal.net

                              #15
                              Re: How do I do date math on 'mm/dd/yyyy'? Example: 10 years earlierthan today.

                              Oli Filth wrote:[color=blue]
                              > john.d.mann@sbc global.net said the following on 13/01/2006 18:34:
                              >[color=green]
                              >> VS wrote:
                              >>[color=darkred]
                              >>> john.d.mann@sbc global.net wrote:
                              >>>
                              >>>> lwoods wrote:
                              >>>>
                              >>>>> I want to calculate and display the 'mm/dd/yyyy' that is 10 years
                              >>>>> earlier than today.
                              >>>>>
                              >>>> // configuration
                              >>>> $date = '01/01/2006';
                              >>>> $offset = '-10';
                              >>>> $date_glue = '/';
                              >>>>
                              >>>> // implementation
                              >>>> $temp = explode($date_g lue, $date);
                              >>>> $temp[2] = $temp[2] + $offset;
                              >>>> $date = implode ($date_glue, $date);
                              >>>> echo $date;
                              >>>
                              >>>
                              >>>
                              >>>
                              >>> That technique only works for some date calculations.
                              >>>
                              >>> Try to take off 10 days during the first 10 days of the month or 10
                              >>> months between Jan and October and you'll get strange results.
                              >>>[/color]
                              >>
                              >> Well, sure, but he was only asking about the year hehe. For the day
                              >> or month, a more advanced version would be required.
                              >>[/color]
                              >
                              > How about 29/02/2004?
                              >
                              >[/color]

                              hehe - Well, the title of the thread says mm/dd/yyyy. So, again, i was
                              just going by the data given by the thread starter. You can always
                              adapt your code to handle the format of dd/mm/yyyy. Something like this...

                              // configuration
                              $date = '01/01/2006';
                              $offset = '-10';
                              $date_glue = '/';

                              // change if your month is in a different position, such as dd/mm/yy
                              $month_location = '1';
                              $day_location = '2';

                              // implementation
                              $temp = explode($date_g lue, $date);
                              if $temp[$day_location - 1] < 11
                              {
                              $eat_previous_m onth_by_this = 10 - $temp[$day_location - 1];
                              switch ($temp[$month_location - 1]) {
                              case 1:
                              $temp[$month_location - 1] = 12;
                              $temp[$day_location - 1] = 31 - $eat_previous_m onth_by_this;
                              break;
                              case 2:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 31 - $eat_previous_m onth_by_this;
                              break;
                              case 3:
                              $temp[$month_location - 1]--;
                              // check for leap year here. is year divisible by four? if so, then
                              use 29, if not then use 28. a simple if statement should suffice.
                              $temp[$day_location - 1] = 28 - $eat_previous_m onth_by_this;
                              break;
                              case 4:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 31 - $eat_previous_m onth_by_this;
                              break;
                              case 5:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 30 - $eat_previous_m onth_by_this;
                              break;
                              case 6:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 31 - $eat_previous_m onth_by_this;
                              break;
                              case 7:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 30 - $eat_previous_m onth_by_this;
                              break;
                              case 8:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 31 - $eat_previous_m onth_by_this;
                              break;
                              case 9:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 31 - $eat_previous_m onth_by_this;
                              break;
                              case 10:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 30 - $eat_previous_m onth_by_this;
                              break;
                              case 11:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 31 - $eat_previous_m onth_by_this;
                              break;
                              case 12:
                              $temp[$month_location - 1]--;
                              $temp[$day_location - 1] = 30 - $eat_previous_m onth_by_this;
                              break;
                              }
                              }
                              $temp[$day_location - 1] = $temp[$day_location - 1] + $offset;
                              $date = implode ($date_glue, $date);
                              echo $date;


                              Comment

                              Working...