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

How to convert timestamp into mm:dd:yy format.

kamill
P: 71
I have a timestamp type field into my mysql database table. Now threw php i need to change it into mm:dd:yy formate.Threw witch function i can do it. Please help me with an examole. suppose timestamp is this 20070209162211 then how can i convert it into mm:dd:yy formate.
...........
kamill
Feb 9 '07 #1
Share this Question
Share on Google+
11 Replies


stephane
P: 35
date( "d/m/Y", $unix_timestamp);

but 20070209162211 it isn't unix timestamp.
unix timestamp is a number of seconds from 1970 year
Feb 9 '07 #2

stephane
P: 35
20070209162211 is
2007-02-09 16:22:11

it's more simple to convert. split this string by symbols using substr() and its all
Feb 9 '07 #3

100+
P: 136
unix timestamp and value stored in mysql timestamp datatype are different. There is no such function which can convert mysql timestamp value to mm:dd:yy format.

So its better to use sustr() to get year, month, date, hour, minute, seconds then you can use it. the format for mysql timestamp type is YYYYMMDDhhmmss.

susen
Feb 9 '07 #4

stephane
P: 35
unix timestamp and value stored in mysql timestamp datatype are different. There is no such function which can convert mysql timestamp value to mm:dd:yy format.
susen
you are wrong
there is a date() function to convert timestamp to every date format
http://www.php.net/date
Feb 9 '07 #5

sunbin
P: 7
hi,

i have used this code block many times,

Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3. $time_stamp=time(); //gives timestamp
  4. $date_format=date("m:d:y",$time_stamp); // formats timestamp in mm:dd:yy 
  5.  
  6. print $date_format; // results here ... 02 : 11 : 07
  7.  
  8. ?>
Solved ???

Sunbin
Feb 11 '07 #6

100+
P: 136
you are wrong
there is a date() function to convert timestamp to every date format
http://www.php.net/date
date() can accept timestamp parameter if it is only unix timestamp not the value stored in mysql table as timestamp datatype.

Susen
Feb 13 '07 #7

ronverdonk
Expert 2.5K+
P: 4,258
I cannot follow if you had your reply, so I'll throw this one in to convert your YYYYMMDDHHMMSS into MM: DD:YY:

Expand|Select|Wrap|Line Numbers
  1. echo sprintf("%02s:%02s:%02s", substr($date,4,2),substr($date,6,2),substr($date,2,2));
Ronald :cool:
Feb 13 '07 #8

100+
P: 136
I cannot follow if you had your reply, so I'll throw this one in to convert your YYYYMMDDHHMMSS into MM: DD:YY:

Expand|Select|Wrap|Line Numbers
  1. echo sprintf("%02s:%02s:%02s", substr($date,4,2),substr($date,6,2),substr($date,2,2));
Ronald :cool:
I accept this as a good solution.

Susen
Feb 14 '07 #9

ronverdonk
Expert 2.5K+
P: 4,258
I accept this as a good solution.

Susen
I am so glad you do!

Ronald :cool:
Feb 14 '07 #10

P: 1
I just learned how to do this properly...
What you want to do is have it all handled in your MySQL Query...


In my case, which might not work for you, I have:

Expand|Select|Wrap|Line Numbers
  1. $select = array();
  2. $select[] = "MONTH(timeStamp) AS theMonth";
  3. $select[] = "DAY(timeStamp) AS theDay";
  4.  
That's just part of my query...

Later, when I want to echo the results, I use:

Expand|Select|Wrap|Line Numbers
  1. <?=$results["theMonth"]."/".$results["theDay"];?>
  2.  
Hopefully this helps. One of our dev. guys just showed it to me and it works like a charm!
Aug 19 '08 #11

P: n/a
are these examples pulling the timestamp from a database row, and displaying in a new format?
Oct 8 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.