473,779 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
10 6189
Rob
setlocale (LC_TIME, "desired timezone")

"point" <po***@caanNOpr oductionSPAM.co m> 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
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",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

Jul 17 '05 #3
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='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 - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #4
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***@glassgian t.com
http://www.glassgiant.com
Jul 17 '05 #5
Thanx guys...

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

Respect..

p
"Shawn Wilson" <sh***@glassgia nt.com> wrote in message
news:3F******** *******@glassgi ant.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***@glassgian t.com
http://www.glassgiant.com

Jul 17 '05 #6
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
mjwilco at yahoo dot com
Essential Tools for the Web Developer - http://mikewilcox.t35.com
Jul 17 '05 #7
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
mjwilco at yahoo dot com
Essential Tools for the Web Developer - http://mikewilcox.t35.com

Jul 17 '05 #8
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
Jul 17 '05 #9
even more interesting :)

......
"Shawn Wilson" <sh***@glassgia nt.com> wrote in message
news:3F******** *******@glassgi ant.com...
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

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11720
by: dan glenn | last post by:
Say, I want to set a cookie and have it expire an hour after it's set. It's looking like this is only possible for browsers which are in the same time zone as my server?? In other words, if I set my cookie with: setcookie('CookieName', $SomeValue, time()+3600, "/");
1
9225
by: Roger Leigh | last post by:
I'd like to use standard C++ to process dates, converting to and from std::string and std::tm representations. The dates are going to/from a database, as ISO-8601 or SQL date-formatted strings. They will also be presented to the user in the correct format for their locale. I can't see a way to use ISO-8601 dates without resorting to non-standard GNU extensions (I'm using GNU libstdc++ and libc). Is this possible? I don't want to tie...
1
3411
by: Bruce Cushman | last post by:
My DB is Access 2000 based "possibly written in 2002" and I have to send and receive update files, ported to a UNIX server by FTP, DB unknown. The file content uses a fixed length string, zero filled left. ie; 001332001695001995040601040630 where, 001332,001695,001995,040601,040630
15
18894
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is
7
4242
by: Jerome | last post by:
Hallo, I know a lot has already been told about date/time fields in a database but still confuses me, specif when dealing with SQLserver(Express). It seems that sqlserver only accepts the date in a "yyyyMMdd" format? (difference between Express and MSDE2000A ?) What is the one and only true way to deal with this problem in VB2005: Local settings are Dutch (Belgium) ; thus date is in "dd/MM/yy" (or perhaps dd/MM/yyyy) and time in...
4
6513
by: AWesner | last post by:
For readability sake I’m going to first state that: LF = Line Feed CHR(10) CR = Carriage Return CHR(13) Since Rich Text Format is a standard formalized by Microsoft Corporation I get to ask them why they decided that RTF format needed to start writing LF instead of the CRLF that you see in implemented in non formatted text such as seen via notepad. Nor can I find any history on why the decision was made. Was it forgotten and just left...
3
2760
by: moni | last post by:
Hi, I wanted to convert a time value in the form of time_t into a readable form in C# or vice versa, in order to be able to subtract two time values and give the result in msecs. eg. I have a time value,
9
12945
by: Alok yadav | last post by:
i am using a webservice in which a method is serach. i use this method which accept a argument of date type in dd/MM/yyyy formate. i have a textbox which accept the date from the user, when i convert textbox data into Datatime formate it converted into MM/dd/yyyy formate, but i have a requirement in dd/MM/yyyy formate. please help me, i am using c#.
5
3224
by: adie | last post by:
hi, can anyone help me out with this. the requirement is to display a time (any time) from a distinct timezone (lets say EST) on a webpage but to make available the conversion to the user local timezone based on the users system time (presume the browser headers contain this) Guess im after a small function that takes a time and converts it to another time based on the users browser system time
10
4026
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one problem. This function does not recognize time zones. How to adjust date to user's time zone? Is converting to timestamp() and then to format readable to visitors the one and only solution (e.g. strtotime() + date() OR DateTime object)? Perhaps,...
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.