refiking Posted October 5, 2008 Share Posted October 5, 2008 I am trying to display the m-d-Y format of a timestamp from a mysql db. Here's my code: while ($raw = mysql_fetch_assoc($dql)){ $date = $raw['exp_date']; } $time = time($date); $exp = date("m-d-Y", $time); Echo $date . '</br>' . $exp; Here's what that returns: 2008-09-30 14:42:25 10-04-2008 Link to comment https://forums.phpfreaks.com/topic/127080-solved-date-conversion-in-php/ Share on other sites More sharing options...
Zane Posted October 5, 2008 Share Posted October 5, 2008 This is why $time = time($date); time() doesn't take any arguments..so it will only return the current days unix timestamp.. so you are basically telling $exp to convert TODAY in that m-d-Y format Link to comment https://forums.phpfreaks.com/topic/127080-solved-date-conversion-in-php/#findComment-657360 Share on other sites More sharing options...
JasonLewis Posted October 5, 2008 Share Posted October 5, 2008 You are after strtotime(). Link to comment https://forums.phpfreaks.com/topic/127080-solved-date-conversion-in-php/#findComment-657361 Share on other sites More sharing options...
refiking Posted October 5, 2008 Author Share Posted October 5, 2008 DUH. Here's the sad part about that. I have 4 other strtotime variables in the same script about 60 lines up. LOL. Nothing like a fresh pair of eyes looking at things. Thanks guys! Link to comment https://forums.phpfreaks.com/topic/127080-solved-date-conversion-in-php/#findComment-657363 Share on other sites More sharing options...
PFMaBiSmAd Posted October 5, 2008 Share Posted October 5, 2008 To output a DATE or DATETIME from a database in any format you want, such as m-d-y, use the mysql DATE_FORMAT() function in your query. Why convert to a Unix timestamp only to convert it back to something else when you can get the end result directly out of the query. Link to comment https://forums.phpfreaks.com/topic/127080-solved-date-conversion-in-php/#findComment-657368 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.