Forum Moderators: coopster

Message Too Old, No Replies

Date and Time

Convert date time stamp

         

TheLazyAce

6:06 am on Jan 22, 2011 (gmt 0)

10+ Year Member



I need a little help converting this date/time stamp:

2011-01-21 23:00:00

This is coming from my DB and I would like it converted to this mannor:
Jan 21 2011 23:00 hrs

Needless to say I would like to be able to convert the current Date/Time stamp back to same time stamp I start with above.

The purpose of this I want to record a Last Access on that login and I want the current login wrote back so it is available for the user next one logs in.

Thanks for the help.

Ed

Matthew1980

7:44 pm on Jan 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there TheLazyAce,

This is more of an mysql question because the formatting is done when the query is executed. You need to use the DATE_FORMAT() [dev.mysql.com] function when constructing the query so that the returned data is in your preferred style.

Example:

%W - Full weekday name
%D - Day of the month with English suffix (1st, 2nd)
%M - Full month name
%Y - Year, numeric, four digits

date_column - the column name in the db
formatted_data - temporary name for storing

$sql = "SELECT `somename` DATE_FORMAT(`date_column`, "%W %D %M %Y") AS `formatted_data` FROM `atablename` WHERE ......";

What happens is you reformat the time given according to the new format you have stipulated, this then gets assigned via the AS instruction to the temporary variable - this is then the name you reference when retrieving the data during the _fetch_array() or _fetch_object() loop.

Hopefully you can see where to go from there.

Good luck!

Cheers,
MRb

TheLazyAce

1:52 am on Jan 23, 2011 (gmt 0)

10+ Year Member



Thank you that is very helpful. I will work with this concept. I still have not figure out how to get the Current Data and time when logging in. I need the current time and date to right back to the DB Field $LastAccess. Once I get the $LastAccess info I want to write back the current time and date to the field so it is available the next time the user logs in.

Ed

Matthew1980

12:32 pm on Jan 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oops, this should have read like this:-

$sql = "SELECT `somename`, DATE_FORMAT(`date_column`, "%W %D %M %Y") AS `formatted_data` FROM `atablename` WHERE ......";

I missed the comma off. That would have thrown an error! My bad, I was tired when I posted...

Cheers,
MRb