Hello, I'm an ASP programmer but I have a PHP script but I don't know PHP I have a var that have int value of seconds, such as: $seconds = "150"; PHP: How can I change it to minutes format, like: 150 seconds => 02:30 minutes I will thank everyone who will be able to help me Thanks.
<? $seconds = "150"; $minutes = floor($seconds/60); $secondsleft = $seconds%60; if($minutes<10) $minutes = "0" . $minutes; if($secondsleft<10) $secondsleft = "0" . $secondsleft; echo "$minutes:$secondsleft minutes"; ?> PHP:
<?php function minutes( $seconds ) { return sprintf( "%02.2d:%02.2d", floor( $seconds / 60 ), $seconds % 60 ); } echo minutes( 150 ); ?> PHP:
probably there's a better way...anyway I coded this and works <?PHP function secondMinute($seconds){ /// get minutes $minResult = floor($seconds/60); /// if minutes is between 0-9, add a "0" --> 00-09 if($minResult < 10){$minResult = 0 . $minResult;} /// get sec $secResult = ($seconds/60 - $minResult)*60; /// if secondes is between 0-9, add a "0" --> 00-09 if($secResult < 10){$secResult = 0 . $secResult;} /// return result echo $minResult,":",$secResult; } /// print secondMinute(150); ?> PHP: EDIT: there was no replies when I started to code it eheh, but now there are better ones
dpfreaks, thanks but I also wanted "0" before the time if it's lower than 10 minutes. legend2, that's what I wanted. You just forgot to add "s" to the "second" var in your second line, I've fixed it and now it's working GREAT!! krakjoe and andrews, Your code is also what I wanted. Very easy to understand & usage. Thanks! Thank you all my friends for your quick help! I've added to each of you a Rep
If there is a function for something, like formatting strings for instance, you should use it, there is less margin for error when you utilize functions that are tried and tested ... plus it's already been established by programmers far more experienced than yourself ( the php authors ) that this is the optimal way of carrying out a task ...
...quick and dirty 1 line version: $seconds = '150'; echo (($min=floor($seconds/60))<10?'0':'').$min.":".(($sec=$seconds`)<10?'0':'').$sec;
i am getting time in fraction like this 21.2...by using your code how can i get time in minute and second format.