By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

time format conversion

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Rob
setlocale (LC_TIME, "desired timezone")

"point" <po***@caanNOproductionSPAM.com> schreef in bericht
news:bq********@enews3.newsguy.com...
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

Jul 17 '05 #2

P: n/a
point wrote:
Hi...

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

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


Try this code snippet...

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

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

print "Before = {$before}<br>After = {$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,Domains,Spam/virus blocking,POP3/IMAP/SMTP auth
SSH&SCP/FTP/FP2k,logs,stats & more! http://www.lohost.com

Jul 17 '05 #3

P: n/a
point wrote:
I have this format 2003-12-01 18:46:20.0

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


<?php
$old_format='2003-12-01 18:46:20.0';
$new_format=date('m-d-Y
H:i:s',strtotime(substr($old_format,0,strpos($old_ format,'.'))));
echo $new_format;
?>

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

Jul 17 '05 #4

P: n/a
point wrote:

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....


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
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #5

P: n/a
Thanx guys...

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

Respect..

p
"Shawn Wilson" <sh***@glassgiant.com> wrote in message
news:3F***************@glassgiant.com...
point wrote:

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....
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
sh***@glassgiant.com
http://www.glassgiant.com

Jul 17 '05 #6

P: n/a
point <po***@caanNOproductionSPAM.com> wrote:
Thanx guys...

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


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
Jul 17 '05 #7

P: n/a
hm..interesting...

thanx fo rthat...
"Michael Wilcox" <mj*****************@yahoo.com> wrote in message
news:i0***************@newsread2.news.atl.earthlin k.net...
point <po***@caanNOproductionSPAM.com> wrote:
Thanx guys...

I went for the Shawn's solution....nice one :)
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

Jul 17 '05 #8

P: n/a
point wrote:

hm..interesting...

thanx fo rthat...

"Michael Wilcox" <mj*****************@yahoo.com> wrote in message
news:i0***************@newsread2.news.atl.earthlin k.net...
point <po***@caanNOproductionSPAM.com> wrote:
Thanx guys...

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


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

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.31105196475983 seconds
Kelv's date() - 0.41387701034546 seconds
Justin's date() - 0.44852697849274 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($arrDates, 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',strtotime(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",strtotime(substr($var,0,19)));

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

echo "<br><Br>Kelv'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
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #9

P: n/a
even more interesting :)

......
"Shawn Wilson" <sh***@glassgiant.com> wrote in message
news:3F***************@glassgiant.com...
point wrote:

hm..interesting...

thanx fo rthat...

"Michael Wilcox" <mj*****************@yahoo.com> wrote in message
news:i0***************@newsread2.news.atl.earthlin k.net...
point <po***@caanNOproductionSPAM.com> wrote:
> Thanx guys...
>
> I went for the Shawn's solution....nice one :)

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

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.31105196475983 seconds
Kelv's date() - 0.41387701034546 seconds
Justin's date() - 0.44852697849274 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($arrDates, 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',strtotime(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",strtotime(substr($var,0,19)));

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

echo "<br><Br>Kelv'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
sh***@glassgiant.com
http://www.glassgiant.com

Jul 17 '05 #10

P: n/a
Great Shawn..... I tested script on my system and found the function
with preg_replace fastest.
-- Rahul
"point" <po***@caanNOproductionSPAM.com> wrote in message news:<br*********@enews2.newsguy.com>...
even more interesting :)

.....
"Shawn Wilson" <sh***@glassgiant.com> wrote in message
news:3F***************@glassgiant.com...
point wrote:

hm..interesting...

thanx fo rthat...

"Michael Wilcox" <mj*****************@yahoo.com> wrote in message
news:i0***************@newsread2.news.atl.earthlin k.net...
> point <po***@caanNOproductionSPAM.com> wrote:
> > Thanx guys...
> >
> > I went for the Shawn's solution....nice one :)
>
> 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

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.31105196475983 seconds
Kelv's date() - 0.41387701034546 seconds
Justin's date() - 0.44852697849274 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($arrDates, 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',strtotime(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",strtotime(substr($var,0,19)));

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

echo "<br><Br>Kelv'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
sh***@glassgiant.com
http://www.glassgiant.com

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.