Padding a time on the left with sprintf? - php

I'm trying to pad a time (playtime of an MP3) using sprintf() in PHP.
sprintf("%02d:2d:2d", $time);
The function that returns the time gets, for example, '1:56' if the MP3 is 1 minute 56 seconds long, and that call brings back "01:56:00" (whereas it needs to be 00:01:56). How can I make this work? How can I tell sprintf to pad on the left?
Or is there a better way to go about this? A more appropriate function maybe?
Thanks

I think you'll need to calculate each element separately, so how about:
sprintf("%02d:%02d:%02d", floor($time/3600), floor($time/60)%60, $time%60);

You can use the date-function.
Something like this should work, if $time is in unix timestamp format:
print(date("H:i:s", $time));

you should use strftime($format,$timestamp) ... probably as this:
strftime("%H:%M:%S",$time)
greetz
back2dos

Related

PHP check if a time is larger then or smaller then a specfic time

I know this question was probably asked,
however i did not find the answer anywhere.
I would like to determine if the current time is before or after 10:30 AM with php. i found a long way to do it that involves a lot of time parsing. but i am sure there are better ways of doing it.
In this answer it show how to do it if the hour is a whole number (example 2PM)
if( 'now' is larger then 'a given time' ){
}
Use time(), date() and strtotime() functions:
if(time() > strtotime(date('Y-m-d').' 10:30') {
//...
}
Use strtotime(). But remember it accept date object.
if(strtotime(dateobject) > strtotime(dateobject)){
//
}

PHP: how convert this time string?

i would convert this time (strftime?)
2013-07-12T07:59:27+0000
in a more simple to read "12/07/2013" (dd-mm-aaaa) and with +2 (i'm in Italy, so this time must be 09:59, no importance for seconds).
Thank you very much!
PS Could you tell me what "kinda of time expression" is, so next time i'll be more capable to search in Google without ask? :)
Try this:
echo date('d/m/Y H:i', strtotime('2013-07-12T07:59:27+0000'));
For more, read this: http://php.net/manual/en/function.strtotime.php

Always display time as XX:XX using PHP

All,
I have the following code to figure out a time based on milliseconds that were provided:
$ms = $value['trackTimeMillis'];
$track_time = floor($ms/60000).':'.floor(($ms%60000)/1000);
The issue is that sometimes this doesn't work that well. For example, if I have the milliseconds as 246995 this will output 4:6.
Is there a way to always make it so that it converts this correct and if it does round to an even number to add a zero at the end of it? So something like 2:3 would read 2:30?
Thanks!
Yes:
sprintf("%d:%02d", floor($ms / 60000), floor($ms % 60000) / 1000);

PHP Date to iCal date format for DTSTART

Is there an easy way to get the correct format for an iCal DTSTART using php date?
I need the format to look like: 20111008T110000 or 20111008 (that one is easy) if I don't have any time.
Does PHP date have a quick way to do this, specifically one that adds the time or removes it when needed?
There isn't any native PHP function or date format that I'm aware of, so you'll need to create your own function. Something like this:
function getIcalDate($time, $inclTime = true)
{
return date('Ymd' . ($inclTime ? '\THis' : ''), $time);
}
As hakre pointed out in a comment, the date formatter can't distinguish between a date with time and a date without time - you'll have to decide the logic behind that.
date('Ymd\THis', time())
You can replace time() with your own timestamp.
This is might be you are looking for link and this also can help you.
mktime function to make timestamp as you want including time or only date to it.

How to extract only Hour data from PHP Time string?

I have a collection of time records in a database in the format '09:51:06' (hour, minute, second).
I have a query which retrieves a bunch of these times from the database, but I only care for the Hour reference.
How can I get rid of the rest of the string and ad just the hour into an array? So in the example above, I just want to keep '09'.
I've looked into exploding and substrings, but can't seem to figure it out.
Thanks.
Exploding the string would look like this (you probably want to add intval() to be able to use it as a real number):
$hours = array_shift(explode(':', '09:51:06'));
But you are probably looking for the right query instead of doing this afterwards. If you are dealing with a time-type, you can use MySQL's date and time functions (HOUR() in this case):
SELECT HOUR(`time_column`) AS `hour` FROM `your_table`
Or from the database itself
SELECT TIME_FORMAT(field_name, '%H') as return_hour from table_name
$time_string='09:51:06';
$your_array[$array_index]=substr($time_string, 0, 2);
for more info on substr
I guess you can do this either thru string manipulation or time/date. Any worthwhile date way would be on the SQL side, PHP has mktime and date functions but you would have get the hour you're looking for intermediately along the way if you were to construct a date anyway
String
$thisHour = array_shift(explode(':', $timeString)); //this gets hour
$thisHour - substr($timeString, 0, 2); // or this
$hours[] = $thisHour; //this adds to array

Categories