Jump to content

How to change the time in .php


Recommended Posts

Hello, i have a question about how to change from database a row wich contains the duration of videos.


I have videos with time in table row duration like this here below:


Table: Duration



300        this should be like this > 3:00 < min


2000      this should be like this > 20:00 < 20 min

10000    this should be like this > 1:00:00 < 1 hour

and so on.


But when i echo out it show me like in the table row exactly.


here is an exmple of my scrip:

$thumbs = $runrows ['thumbs'];
$title = $runrows ['title'];
$url = $runrows ['url'];
$duration = $runrows ['duration'];

 while($runrows = mysql_fetch_assoc($getquery))
echo '<li class="thumbcontent"><a href="'. $runrows['url'] .'" ><img src="'. $runrows['thumbs'].'" class="thumb" name="'. $runrows['title'] .'" alt="'. $runrows['title'] .'" title="'. $runrows['title'] .'" width="240" height="180" />		  

<span class="duration">'. $runrows['duration'].'</span></li>
echo "<center>";


And this is what a friend send me to check out but i cant figure out how to fit this code with my own code.

foreach (array(500, 234, 1100, 520, 1300, 10000) as $number) {
    echo $number, "\t";
    $number = str_pad($number, 6, 0, STR_PAD_LEFT);
    echo ltrim($number{0}.$number{1}.':'.$number{2}.$number{3}.':'.$number{4}.$number{5}, ":0"), "\n";

or this:

foreach (['500', '234', '1100', '520', '1300', '10000'] as $number) {
    $time = strrev($number);
    $time = wordwrap($time, 2, ':', true);
    $time = strrev($time);
    echo "$number was transformed to $time\n";

I hope i explained it well, i hope somebody can help me with a solution.


Thanks in advance.

Link to comment
Share on other sites

You should really be using a TIME data type for your duration column in the database.


But to answer your question you can make your friends code into a function

function convertToTime($number)
    $time = strrev($number);
    $time = wordwrap($time, 2, ':', true);
    return  strrev($time);

Then when you want to convert the duration you'd call the function

echo '<li class="thumbcontent">...

<span class="duration">'. convertToTime($runrows['duration']).'</span></li>
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.

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.