Timestamp Conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Timestamp Conversion

Post by AliasBDI »

I have always had problems with converting timestamp and datetime. I echo (from a MySQL database) the timestamp field and it give me this: 1143930624. How do I know whether this is a UNIX TIMESTAMP or not? And then how do I convert it? :?:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

AliasBDI wrote:1143930624
is a unix timestamp. Why? it's too large for a "standard" textual one (often in the format YYYYMMDD or YYYY-MM-DD)

Now, how does one convert it? Convert it to what? To MySQL standard notation for a DATE field, date('Y-m-d', 1143930624); for a DATETIME field date('Y-m-d H:i:s', 1143930624); for a TIMESTAMP date('YmdHis', 1143930624); for a TIME field date('H:i:s', 1143930624), etc etc.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I'm wanting to echo it out to 'MM-DD-YYYY'. I tried this date('m-d-Y', $row_conDETAILS['createDATE']); but it does not work. It gives me '01-18-2038'. It should give '04-01-2006'.

*It looks like when I originally posted my full echo of the timestamp did not post. Nevertheless, according to your standards, it is still a timestamp. This is what it echos with no MySQL query conversion: '20060401163024'. Does it matter that this is a Windows Server?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

20060401163024 is not a unix timestamp. It is a standard MySQL TIMESTAMP field. I'd suggest using DATE_FORMAT() found in MySQL to make it the format you wish.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Great, works. I put this in my SELECT query: 'DATE_FORMAT(createDATE,'%m-%d-%Y') AS createDATE'. Would this be the same concept for a datetime field instead of a timestamp?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, it the same sort of thing.
Post Reply