time format conversion

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

    time format conversion

    Hi...

    I have this format 2003-12-01 18:46:20.0

    And would like it to be 01-12-2003 18:46:20

    I know it can be done vis string cuts and explodes but ther's got to be a
    better way using time functions....

    Any help would be appriciated..

    Thanx


  • Rob

    #2
    Re: time format conversion

    setlocale (LC_TIME, "desired timezone")

    "point" <point@caanNOpr oductionSPAM.co m> schreef in bericht
    news:bqq10v0l5d @enews3.newsguy .com...[color=blue]
    > Hi...
    >
    > I have this format 2003-12-01 18:46:20.0
    >
    > And would like it to be 01-12-2003 18:46:20
    >
    > I know it can be done vis string cuts and explodes but ther's got to be a
    > better way using time functions....
    >
    > Any help would be appriciated..
    >
    > Thanx
    >
    >[/color]


    Comment

    • Kelv

      #3
      Re: time format conversion

      point wrote:[color=blue]
      > Hi...
      >
      > I have this format 2003-12-01 18:46:20.0
      >
      > And would like it to be 01-12-2003 18:46:20[/color]

      Try this code snippet...

      $before = "2003-12-01 18:46:20.0";

      $after = date("d-m-Y H:i:s",strtotim e(substr($befor e,0,19)));

      print "Before = {$before}<br>Af ter = {$after}";

      The .0 on your date threw strtotime, hence the substr.

      I hope that helps,

      Kelv :)

      ----
      LoHost - Low cost high uptime Internet Services. UK & USA
      PHP,MySQL,Domai ns,Spam/virus blocking,POP3/IMAP/SMTP auth
      SSH&SCP/FTP/FP2k,logs,stats & more! http://www.lohost.com

      Comment

      • Justin Koivisto

        #4
        Re: time format conversion

        point wrote:[color=blue]
        > I have this format 2003-12-01 18:46:20.0
        >
        > And would like it to be 01-12-2003 18:46:20[/color]

        <?php
        $old_format='20 03-12-01 18:46:20.0';
        $new_format=dat e('m-d-Y
        H:i:s',strtotim e(substr($old_f ormat,0,strpos( $old_format,'.' ))));
        echo $new_format;
        ?>

        --
        Justin Koivisto - spam@koivi.com
        PHP POSTERS: Please use comp.lang.php for PHP related questions,
        alt.php* groups are not recommended.

        Comment

        • Shawn Wilson

          #5
          Re: time format conversion

          point wrote:[color=blue]
          >
          > Hi...
          >
          > I have this format 2003-12-01 18:46:20.0
          >
          > And would like it to be 01-12-2003 18:46:20
          >
          > I know it can be done vis string cuts and explodes but ther's got to be a
          > better way using time functions....[/color]

          You have this as a string?

          If so, something like

          $date = preg_replace("/^(\d+)-(\d+)-(\d+) (\d+:\d+:\d+)(\ .\d*)?$/", "\$3-\$2-\$1
          \$4", $date);

          should work, but I haven't tested it.

          Regards,
          Shawn
          --
          Shawn Wilson
          shawn@glassgian t.com

          Comment

          • point

            #6
            Re: time format conversion

            Thanx guys...

            I went for the Shawn's solution....nic e one :)

            Respect..

            p


            "Shawn Wilson" <shawn@glassgia nt.com> wrote in message
            news:3FD09706.2 C8DAB80@glassgi ant.com...[color=blue]
            > point wrote:[color=green]
            > >
            > > Hi...
            > >
            > > I have this format 2003-12-01 18:46:20.0
            > >
            > > And would like it to be 01-12-2003 18:46:20
            > >
            > > I know it can be done vis string cuts and explodes but ther's got to be[/color][/color]
            a[color=blue][color=green]
            > > better way using time functions....[/color]
            >
            > You have this as a string?
            >
            > If so, something like
            >
            > $date = preg_replace("/^(\d+)-(\d+)-(\d+) (\d+:\d+:\d+)(\ .\d*)?$/",[/color]
            "\$3-\$2-\$1[color=blue]
            > \$4", $date);
            >
            > should work, but I haven't tested it.
            >
            > Regards,
            > Shawn
            > --
            > Shawn Wilson
            > shawn@glassgian t.com
            > http://www.glassgiant.com[/color]


            Comment

            • Michael Wilcox

              #7
              Re: time format conversion

              point <point@caanNOpr oductionSPAM.co m> wrote:[color=blue]
              > Thanx guys...
              >
              > I went for the Shawn's solution....nic e one :)[/color]

              Shawn's solution, while valid, can be heavy on server resources and take
              longer. strtotime() does just what you want and returns the timestamp, which
              can be used with date()
              --
              Michael Wilcox
              mjwilco at yahoo dot com
              Essential Tools for the Web Developer - http://mikewilcox.t35.com


              Comment

              • point

                #8
                Re: time format conversion

                hm..interesting ...

                thanx fo rthat...


                "Michael Wilcox" <mjwilcoCANTHAV ESPAM@yahoo.com > wrote in message
                news:i07Ab.703$ 7p2.518@newsrea d2.news.atl.ear thlink.net...[color=blue]
                > point <point@caanNOpr oductionSPAM.co m> wrote:[color=green]
                > > Thanx guys...
                > >
                > > I went for the Shawn's solution....nic e one :)[/color]
                >
                > Shawn's solution, while valid, can be heavy on server resources and take
                > longer. strtotime() does just what you want and returns the timestamp,[/color]
                which[color=blue]
                > can be used with date()
                > --
                > Michael Wilcox
                > mjwilco at yahoo dot com
                > Essential Tools for the Web Developer - http://mikewilcox.t35.com
                >
                >[/color]


                Comment

                • Shawn Wilson

                  #9
                  Re: time format conversion

                  point wrote:[color=blue]
                  >
                  > hm..interesting ...
                  >
                  > thanx fo rthat...
                  >
                  > "Michael Wilcox" <mjwilcoCANTHAV ESPAM@yahoo.com > wrote in message
                  > news:i07Ab.703$ 7p2.518@newsrea d2.news.atl.ear thlink.net...[color=green]
                  > > point <point@caanNOpr oductionSPAM.co m> wrote:[color=darkred]
                  > > > Thanx guys...
                  > > >
                  > > > I went for the Shawn's solution....nic e one :)[/color]
                  > >
                  > > Shawn's solution, while valid, can be heavy on server resources and take
                  > > longer. strtotime() does just what you want and returns the timestamp,[/color]
                  > which[color=green]
                  > > can be used with date()
                  > > --
                  > > Michael Wilcox[/color][/color]


                  Actually, I tested the three solutions presented (mine, Justin's, and Kelv's).
                  Mine was the fastest by a significant amount. I think, probably because you have
                  the search in strtotime(). I would use strtotime() only if you want a timestamp
                  as the final result (ie. for storing in a database). With that said, with any
                  solution it took 1/3 to 1/2 second to convert 10,000 dates. So if you're only
                  doing a few you shouldn't notice any difference.

                  The test was run over 10,000 randomly generated date strings, in the format that
                  Point mentioned. The results were:

                  Shawn's preg() - 0.3110519647598 3 seconds
                  Kelv's date() - 0.4138770103454 6 seconds
                  Justin's date() - 0.4485269784927 4 seconds

                  I ran it a number of times. I changed the order around a bunch of times and in
                  each case the results were similar to that shown above. The code is below if
                  anyone sees a problem with it, let me know. Note: I did this to satisfy my own
                  curiosity, not to be a prick about my solution being critiqued :o)

                  Regards,
                  Shawn

                  <?PHP

                  function getmicrotime(){
                  list($usec, $sec) = explode(" ",microtime ());
                  return ((float)$usec + (float)$sec);
                  }

                  $arrDates = array();

                  for ($i = 0; $i < 10000; ++$i) {
                  array_push($arr Dates, strftime("%Y-%m-%d %H:%M:%S.".rand (0,9),
                  rand(0,time())) );
                  }


                  $time_start = getmicrotime();
                  $date = "";
                  foreach ($arrDates as $var)
                  $date = date('d-m-Y H:i:s',strtotim e(substr($var,0 ,strpos($var,'. '))));

                  $time_end = getmicrotime();
                  $time = $time_end - $time_start;

                  echo "<br><Br>Justin 's date() - $time seconds";





                  $time_start = getmicrotime();
                  $date = "";
                  foreach ($arrDates as $var)
                  $date = date("d-m-Y H:i:s",strtotim e(substr($var,0 ,19)));

                  $time_end = getmicrotime();
                  $time = $time_end - $time_start;

                  echo "<br><Br>Ke lv's date() - $time seconds";





                  $time_start = getmicrotime();
                  $date = "";
                  foreach ($arrDates as $var)
                  $date = preg_replace("/^(\d+)-(\d+)-(\d+) (\d+:\d+:\d+)(\ .\d*)?$/",
                  "\$3-\$2-\$1 \$4, ", $var);
                  $time_end = getmicrotime();
                  $time = $time_end - $time_start;

                  echo "<br><Br>Shawn' s preg() - $time seconds";

                  ?>
                  --
                  Shawn Wilson
                  shawn@glassgian t.com

                  Comment

                  • point

                    #10
                    Re: time format conversion

                    even more interesting :)

                    ......


                    "Shawn Wilson" <shawn@glassgia nt.com> wrote in message
                    news:3FD4917E.2 67387E4@glassgi ant.com...[color=blue]
                    > point wrote:[color=green]
                    > >
                    > > hm..interesting ...
                    > >
                    > > thanx fo rthat...
                    > >
                    > > "Michael Wilcox" <mjwilcoCANTHAV ESPAM@yahoo.com > wrote in message
                    > > news:i07Ab.703$ 7p2.518@newsrea d2.news.atl.ear thlink.net...[color=darkred]
                    > > > point <point@caanNOpr oductionSPAM.co m> wrote:
                    > > > > Thanx guys...
                    > > > >
                    > > > > I went for the Shawn's solution....nic e one :)
                    > > >
                    > > > Shawn's solution, while valid, can be heavy on server resources and[/color][/color][/color]
                    take[color=blue][color=green][color=darkred]
                    > > > longer. strtotime() does just what you want and returns the timestamp,[/color]
                    > > which[color=darkred]
                    > > > can be used with date()
                    > > > --
                    > > > Michael Wilcox[/color][/color]
                    >
                    >
                    > Actually, I tested the three solutions presented (mine, Justin's, and[/color]
                    Kelv's).[color=blue]
                    > Mine was the fastest by a significant amount. I think, probably because[/color]
                    you have[color=blue]
                    > the search in strtotime(). I would use strtotime() only if you want a[/color]
                    timestamp[color=blue]
                    > as the final result (ie. for storing in a database). With that said, with[/color]
                    any[color=blue]
                    > solution it took 1/3 to 1/2 second to convert 10,000 dates. So if you're[/color]
                    only[color=blue]
                    > doing a few you shouldn't notice any difference.
                    >
                    > The test was run over 10,000 randomly generated date strings, in the[/color]
                    format that[color=blue]
                    > Point mentioned. The results were:
                    >
                    > Shawn's preg() - 0.3110519647598 3 seconds
                    > Kelv's date() - 0.4138770103454 6 seconds
                    > Justin's date() - 0.4485269784927 4 seconds
                    >
                    > I ran it a number of times. I changed the order around a bunch of times[/color]
                    and in[color=blue]
                    > each case the results were similar to that shown above. The code is below[/color]
                    if[color=blue]
                    > anyone sees a problem with it, let me know. Note: I did this to satisfy[/color]
                    my own[color=blue]
                    > curiosity, not to be a prick about my solution being critiqued :o)
                    >
                    > Regards,
                    > Shawn
                    >
                    > <?PHP
                    >
                    > function getmicrotime(){
                    > list($usec, $sec) = explode(" ",microtime ());
                    > return ((float)$usec + (float)$sec);
                    > }
                    >
                    > $arrDates = array();
                    >
                    > for ($i = 0; $i < 10000; ++$i) {
                    > array_push($arr Dates, strftime("%Y-%m-%d %H:%M:%S.".rand (0,9),
                    > rand(0,time())) );
                    > }
                    >
                    >
                    > $time_start = getmicrotime();
                    > $date = "";
                    > foreach ($arrDates as $var)
                    > $date = date('d-m-Y H:i:s',strtotim e(substr($var,0 ,strpos($var,'. '))));
                    >
                    > $time_end = getmicrotime();
                    > $time = $time_end - $time_start;
                    >
                    > echo "<br><Br>Justin 's date() - $time seconds";
                    >
                    >
                    >
                    >
                    >
                    > $time_start = getmicrotime();
                    > $date = "";
                    > foreach ($arrDates as $var)
                    > $date = date("d-m-Y H:i:s",strtotim e(substr($var,0 ,19)));
                    >
                    > $time_end = getmicrotime();
                    > $time = $time_end - $time_start;
                    >
                    > echo "<br><Br>Ke lv's date() - $time seconds";
                    >
                    >
                    >
                    >
                    >
                    > $time_start = getmicrotime();
                    > $date = "";
                    > foreach ($arrDates as $var)
                    > $date = preg_replace("/^(\d+)-(\d+)-(\d+) (\d+:\d+:\d+)(\ .\d*)?$/",
                    > "\$3-\$2-\$1 \$4, ", $var);
                    > $time_end = getmicrotime();
                    > $time = $time_end - $time_start;
                    >
                    > echo "<br><Br>Shawn' s preg() - $time seconds";
                    >
                    > ?>
                    > --
                    > Shawn Wilson
                    > shawn@glassgian t.com
                    > http://www.glassgiant.com[/color]


                    Comment

                    • Rahul Anand

                      #11
                      Re: time format conversion

                      Great Shawn..... I tested script on my system and found the function
                      with preg_replace fastest.


                      -- Rahul


                      "point" <point@caanNOpr oductionSPAM.co m> wrote in message news:<br7e3402v rj@enews2.newsg uy.com>...[color=blue]
                      > even more interesting :)
                      >
                      > .....
                      >
                      >
                      > "Shawn Wilson" <shawn@glassgia nt.com> wrote in message
                      > news:3FD4917E.2 67387E4@glassgi ant.com...[color=green]
                      > > point wrote:[color=darkred]
                      > > >
                      > > > hm..interesting ...
                      > > >
                      > > > thanx fo rthat...
                      > > >
                      > > > "Michael Wilcox" <mjwilcoCANTHAV ESPAM@yahoo.com > wrote in message
                      > > > news:i07Ab.703$ 7p2.518@newsrea d2.news.atl.ear thlink.net...
                      > > > > point <point@caanNOpr oductionSPAM.co m> wrote:
                      > > > > > Thanx guys...
                      > > > > >
                      > > > > > I went for the Shawn's solution....nic e one :)
                      > > > >
                      > > > > Shawn's solution, while valid, can be heavy on server resources and[/color][/color]
                      > take[color=green][color=darkred]
                      > > > > longer. strtotime() does just what you want and returns the timestamp,[/color][/color]
                      > which[color=green][color=darkred]
                      > > > > can be used with date()
                      > > > > --
                      > > > > Michael Wilcox[/color]
                      > >
                      > >
                      > > Actually, I tested the three solutions presented (mine, Justin's, and[/color]
                      > Kelv's).[color=green]
                      > > Mine was the fastest by a significant amount. I think, probably because[/color]
                      > you have[color=green]
                      > > the search in strtotime(). I would use strtotime() only if you want a[/color]
                      > timestamp[color=green]
                      > > as the final result (ie. for storing in a database). With that said, with[/color]
                      > any[color=green]
                      > > solution it took 1/3 to 1/2 second to convert 10,000 dates. So if you're[/color]
                      > only[color=green]
                      > > doing a few you shouldn't notice any difference.
                      > >
                      > > The test was run over 10,000 randomly generated date strings, in the[/color]
                      > format that[color=green]
                      > > Point mentioned. The results were:
                      > >
                      > > Shawn's preg() - 0.3110519647598 3 seconds
                      > > Kelv's date() - 0.4138770103454 6 seconds
                      > > Justin's date() - 0.4485269784927 4 seconds
                      > >
                      > > I ran it a number of times. I changed the order around a bunch of times[/color]
                      > and in[color=green]
                      > > each case the results were similar to that shown above. The code is below[/color]
                      > if[color=green]
                      > > anyone sees a problem with it, let me know. Note: I did this to satisfy[/color]
                      > my own[color=green]
                      > > curiosity, not to be a prick about my solution being critiqued :o)
                      > >
                      > > Regards,
                      > > Shawn
                      > >
                      > > <?PHP
                      > >
                      > > function getmicrotime(){
                      > > list($usec, $sec) = explode(" ",microtime ());
                      > > return ((float)$usec + (float)$sec);
                      > > }
                      > >
                      > > $arrDates = array();
                      > >
                      > > for ($i = 0; $i < 10000; ++$i) {
                      > > array_push($arr Dates, strftime("%Y-%m-%d %H:%M:%S.".rand (0,9),
                      > > rand(0,time())) );
                      > > }
                      > >
                      > >
                      > > $time_start = getmicrotime();
                      > > $date = "";
                      > > foreach ($arrDates as $var)
                      > > $date = date('d-m-Y H:i:s',strtotim e(substr($var,0 ,strpos($var,'. '))));
                      > >
                      > > $time_end = getmicrotime();
                      > > $time = $time_end - $time_start;
                      > >
                      > > echo "<br><Br>Justin 's date() - $time seconds";
                      > >
                      > >
                      > >
                      > >
                      > >
                      > > $time_start = getmicrotime();
                      > > $date = "";
                      > > foreach ($arrDates as $var)
                      > > $date = date("d-m-Y H:i:s",strtotim e(substr($var,0 ,19)));
                      > >
                      > > $time_end = getmicrotime();
                      > > $time = $time_end - $time_start;
                      > >
                      > > echo "<br><Br>Ke lv's date() - $time seconds";
                      > >
                      > >
                      > >
                      > >
                      > >
                      > > $time_start = getmicrotime();
                      > > $date = "";
                      > > foreach ($arrDates as $var)
                      > > $date = preg_replace("/^(\d+)-(\d+)-(\d+) (\d+:\d+:\d+)(\ .\d*)?$/",
                      > > "\$3-\$2-\$1 \$4, ", $var);
                      > > $time_end = getmicrotime();
                      > > $time = $time_end - $time_start;
                      > >
                      > > echo "<br><Br>Shawn' s preg() - $time seconds";
                      > >
                      > > ?>
                      > > --
                      > > Shawn Wilson
                      > > shawn@glassgian t.com
                      > > http://www.glassgiant.com[/color][/color]

                      Comment

                      Working...