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
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

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.