Jump to content

How to change the time in .php


Tasos

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

 

5:00

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

10:00

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:

<?php   
$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" />		  
</a>

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

<?php
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
https://forums.phpfreaks.com/topic/287821-how-to-change-the-time-in-php/
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>
     ';

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.