Date formatting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Lucas-Smith

    Date formatting



    I have a string saved in the format produced by date ('Ymd-Hms')

    e.g.

    20050215-130257

    Can anyone suggest the easiest way to present this as e.g.

    13.02pm, 15/Feb/2005

    ?

    Things like strftime () don't seem to be applicable, and it's not
    obviously how the general string formatting functions could be applied
    here.


    Martin
  • Steve

    #2
    Re: Date formatting


    Nasty format... needs to be taken apart and put back in some parsable
    format, such as this:

    <?php

    // example date in custom format...
    $t_strCustomDat e = "20050215-130257";

    // disassemble...
    $t_strYear = substr($t_strCu stomDate, 0, 4);
    $t_strMonth = substr($t_strCu stomDate, 4, 2);
    $t_strDay = substr($t_strCu stomDate, 6, 2);
    $t_strHour = substr($t_strCu stomDate, 9, 2);
    $t_strMinute = substr($t_strCu stomDate, 11, 2);
    $t_strSecond = substr($t_strCu stomDate, 13, 2);

    // reassemble...
    $t_strCustomDat e = $t_strYear . "-" . $t_strMonth . "-" . $t_strDay . "
    " . $t_strHour . ":" . $t_strMinute . ":" . $t_strSecond;

    // now it looks like this...
    //$t_strCustomDat e = "2005-02-15 13:02:57";

    print date( "H.ma, d/M/Y ", strtotime($t_st rCustomDate) );

    ?>

    ---
    Steve

    Comment

    • Janwillem Borleffs

      #3
      Re: Date formatting

      Martin Lucas-Smith wrote:[color=blue]
      > I have a string saved in the format produced by date ('Ymd-Hms')
      >
      > e.g.
      >
      > 20050215-130257
      >
      > Can anyone suggest the easiest way to present this as e.g.
      >
      > 13.02pm, 15/Feb/2005
      >
      > ?[/color]

      $ts = preg_replace(
      "/-(\d{2})(\d{2})( \d{2})$/",
      " $1:$2:$3",
      "20050215-130257"
      );

      print date("H.ma, d/M/Y", strtotime($ts)) ;


      JW



      Comment

      • Martin Lucas-Smith

        #4
        Re: Date formatting



        Thanks for the replies:
        [color=blue][color=green]
        > > I have a string saved in the format produced by date ('Ymd-Hms') e.g.
        > > 20050215-130257 . Can anyone suggest the easiest way to present this
        > > as e.g. 13.02pm, 15/Feb/2005 ?[/color][/color]


        On Wed, 16 Feb 2005, Steve wrote:[color=blue]
        > Nasty format...[/color]

        Basically the format had been chosen to append a timestamp to a flat-file
        database (using the OS timestamp isn't possible because there could be
        multiple instances of the same name). The format basically ensures that
        the filenames will be in the correct alphabetical order, as the date is
        effectively 'backwards'.


        On Wed, 16 Feb 2005, Janwillem Borleffs wrote:

        [color=blue]
        > $ts = preg_replace(
        > "/-(\d{2})(\d{2})( \d{2})$/",
        > " $1:$2:$3",
        > "20050215-130257"
        > );
        >
        > print date("H.ma, d/M/Y", strtotime($ts)) ;[/color]

        Thanks - this use of preg_replace is an elegant solution which I'd
        overlooked.


        Martin

        Comment

        • Rodney Pont

          #5
          Re: Date formatting

          On Wed, 16 Feb 2005 16:33:49 +0000, Martin Lucas-Smith wrote:
          [color=blue]
          >
          >
          >I have a string saved in the format produced by date ('Ymd-Hms')
          >
          >e.g.
          >
          >20050215-130257
          >
          >Can anyone suggest the easiest way to present this as e.g.
          >
          >13.02pm, 15/Feb/2005[/color]

          Just wondering if you really want the month to be shown as the minute.
          The minute indicator is i, m is for the numeric month.

          So ,using the date and time of your post as the example (at the top)
          date('Ymd-Hms') gives 20050216-160249
          date('Ymd-His') gives 20050216-163349

          You may really want the month but if not this will save you some bug
          hunting :-)

          --
          Regards - Rodney Pont
          The from address exists but is mostly dumped,
          please send any emails to the address below
          e-mail ngpsm4 (at) infohitsystems (dot) ltd (dot) uk


          Comment

          • Martin Lucas-Smith

            #6
            Re: Date formatting



            On Thu, 17 Feb 2005, Rodney Pont wrote:
            [color=blue]
            > On Wed, 16 Feb 2005 16:33:49 +0000, Martin Lucas-Smith wrote:
            >[color=green]
            > >
            > >
            > >I have a string saved in the format produced by date ('Ymd-Hms')
            > >
            > >e.g.
            > >
            > >20050215-130257
            > >
            > >Can anyone suggest the easiest way to present this as e.g.
            > >
            > >13.02pm, 15/Feb/2005[/color]
            >
            > Just wondering if you really want the month to be shown as the minute.
            > The minute indicator is i, m is for the numeric month.
            >
            > So ,using the date and time of your post as the example (at the top)
            > date('Ymd-Hms') gives 20050216-160249
            > date('Ymd-His') gives 20050216-163349
            >
            > You may really want the month but if not this will save you some bug
            > hunting :-)[/color]

            Thanks - yep, realised this yesterday, when the files started being
            written out of sequence...


            Martin

            Comment

            Working...