1042 Posted September 4, 2007 Share Posted September 4, 2007 Im pulling information from an xml file tha returns the time formatted like this 161322, how would i format the result like this, 16:13:22 ? i tried: $thetime="160000"; echo date('H:i:s A', strtotime($thetime)); but all i get is 00:00:00 AM any help is greatly appreciated, thanks. Link to comment https://forums.phpfreaks.com/topic/67969-number-formatted-for-time/ Share on other sites More sharing options...
Fadion Posted September 4, 2007 Share Posted September 4, 2007 It works for me, it is printing "16:00:00 PM". Link to comment https://forums.phpfreaks.com/topic/67969-number-formatted-for-time/#findComment-341669 Share on other sites More sharing options...
1042 Posted September 4, 2007 Author Share Posted September 4, 2007 It works for me, it is printing "16:00:00 PM". That's weird, im using php 4.4.7, would that be an issue? Link to comment https://forums.phpfreaks.com/topic/67969-number-formatted-for-time/#findComment-341674 Share on other sites More sharing options...
cooldude832 Posted September 5, 2007 Share Posted September 5, 2007 sprintf() is a function that by designs returns a formated string. http://us2.php.net/manual/en/function.sprintf.php That will probably help you, that or if it carries non significant zeros i.e (01:01:01) than you could just explode it to 2 character parts then regule it together with colons Link to comment https://forums.phpfreaks.com/topic/67969-number-formatted-for-time/#findComment-341690 Share on other sites More sharing options...
1042 Posted September 5, 2007 Author Share Posted September 5, 2007 sprintf() is a function that by designs returns a formated string. http://us2.php.net/manual/en/function.sprintf.php That will probably help you, that or if it carries non significant zeros i.e (01:01:01) than you could just explode it to 2 character parts then regule it together with colons this is what i ended up doing; $number="161321"; $number = substr(chunk_split($number, 2, ':'), 0, -1); //If $number is '1234567', result is '1 234 567'. //echo $number; echo date('h:i:s ', strtotime($number)); is there a better way? Link to comment https://forums.phpfreaks.com/topic/67969-number-formatted-for-time/#findComment-341703 Share on other sites More sharing options...
cooldude832 Posted September 5, 2007 Share Posted September 5, 2007 Its a little burden some if you are only returning the string from xml for output (and not using as a time) I thought of an alternative solution that will work for you using chunk_split() <?php $string = 161513 //16:15:13 is what we are seeking as the end result $date = chunk_split($string,6,":"); echo $date; //Echoes 16:15:13 ?> I really don't use sprintf() as I'm not a old school C programer where most of that formatted string stuff came from. Link to comment https://forums.phpfreaks.com/topic/67969-number-formatted-for-time/#findComment-341712 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.