paul.mearing Posted May 9, 2006 Share Posted May 9, 2006 Hi,I have outputs from a sql database which print out in this format:2006-05-09 18:11:20.000 is there a simple way i can format it so it looks like this:2006-05-09 6:20pmAny ideas?Thanks. Quote Link to comment Share on other sites More sharing options...
rt-box Posted May 9, 2006 Share Posted May 9, 2006 [!--quoteo(post=372568:date=May 9 2006, 03:11 PM:name=paul.mearing)--][div class=\'quotetop\']QUOTE(paul.mearing @ May 9 2006, 03:11 PM) [snapback]372568[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hi,I have outputs from a sql database which print out in this format:2006-05-09 18:11:20.000 is there a simple way i can format it so it looks like this:2006-05-09 6:20pmAny ideas?Thanks.[/quote]$output = strftime("Y-m-d I:Mr", strtotime($date_from_db));In versions prior to PHP 5, you'd also have to get rid of the microseconds in the time you get from the database, as it will generate an error in strtotime().For further precision, read the documentation.hth Quote Link to comment Share on other sites More sharing options...
paul.mearing Posted May 9, 2006 Author Share Posted May 9, 2006 [!--quoteo(post=372581:date=May 9 2006, 02:42 PM:name=rt-box)--][div class=\'quotetop\']QUOTE(rt-box @ May 9 2006, 02:42 PM) [snapback]372581[/snapback][/div][div class=\'quotemain\'][!--quotec--]$output = strftime("Y-m-d I:Mr", strtotime($date_from_db));In versions prior to PHP 5, you'd also have to get rid of the microseconds in the time you get from the database, as it will generate an error in strtotime().For further precision, read the documentation.hth[/quote]When i try what you should, I get all my results echo'ing out as "-1". This is my code:[code]$open_date1 = strtotime("Y-m-d I:Mr", strtotime($open_date));[/code]Got any ideas? Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 9, 2006 Share Posted May 9, 2006 [!--quoteo(post=372626:date=May 9 2006, 11:10 AM:name=paul.mearing)--][div class=\'quotetop\']QUOTE(paul.mearing @ May 9 2006, 11:10 AM) [snapback]372626[/snapback][/div][div class=\'quotemain\'][!--quotec--]When i try what you should, I get all my results echo'ing out as "-1". This is my code:[code]$open_date1 = strtotime("Y-m-d I:Mr", strtotime($open_date));[/code]Got any ideas?[/quote]you're close, but he's recommended using strftime() on the outside, not strtotime(). also, you could use date():[code]$open_date1 = date("Y-m-d I:Mr", strtotime($open_date));[/code] Quote Link to comment Share on other sites More sharing options...
paul.mearing Posted May 9, 2006 Author Share Posted May 9, 2006 [!--quoteo(post=372628:date=May 9 2006, 04:12 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 9 2006, 04:12 PM) [snapback]372628[/snapback][/div][div class=\'quotemain\'][!--quotec--]you're close, but he's recommended using strftime() on the outside, not strtotime(). also, you could use date():[code]$open_date1 = date("Y-m-d I:Mr", strtotime($open_date));[/code][/quote]When i try to use:[code]$open_date1 = strftime("Y-m-d I:Mr", strtotime($open_date));[/code]i get blanks results...when i try it with date(), i get the following error:Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\inetpub\wwwroot\magic8\SevBoard\PHPSevboard\sev123brd.php on line 86 Quote Link to comment Share on other sites More sharing options...
rt-box Posted May 9, 2006 Share Posted May 9, 2006 [!--quoteo(post=372632:date=May 9 2006, 05:28 PM:name=paul.mearing)--][div class=\'quotetop\']QUOTE(paul.mearing @ May 9 2006, 05:28 PM) [snapback]372632[/snapback][/div][div class=\'quotemain\'][!--quotec--]When i try to use:[code]$open_date1 = strftime("Y-m-d I:Mr", strtotime($open_date));[/code]i get blanks results...when i try it with date(), i get the following error:Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\inetpub\wwwroot\magic8\SevBoard\PHPSevboard\sev123brd.php on line 86[/quote]As I said in my first reply:[!--quoteo(post=372581:date=May 9 2006, 02:42 PM:name=rt-box)--][div class=\'quotetop\']QUOTE(rt-box @ May 9 2006, 02:42 PM) [snapback]372581[/snapback][/div][div class=\'quotemain\'][!--quotec--]In versions prior to PHP 5, you'd also have to get rid of the microseconds in the time you get from the database, as it will generate an error in strtotime().[/quote]So now you know: check the version of PHP you are using, and if prior to version 5, remove the microseconds from the $open_date string:[code]$open_date1 = strftime("Y-m-d I:Mr", strtotime(substr($open_date, 0, strrpos($open_date, "."))));[/code]If I may add - no offense - please read twice and check all the indications given before reposting. And also, do check the [a href=\"http://www.php.net/docs.net\" target=\"_blank\"]PHP reference[/a] before posting as it [i]does[/i] help [i]a lot[/i].hthRT Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.