Jump to content

Number formatted for time


1042

Recommended Posts

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

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

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.