point wrote:
hm..interesting ...
thanx fo rthat...
"Michael Wilcox" <mj************ *****@yahoo.com > wrote in message
news:i0******** *******@newsrea d2.news.atl.ear thlink.net... point <po***@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 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.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
sh***@glassgian t.com http://www.glassgiant.com