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);
Related
For example -
In Jquery using the function (new Date()).getTime() am getting current datetime as 1470291303352.
But In PHP using strtotime(date('H:i:s')) am getting it as 1470291299.
Here i need to get the same string values. How to do it?
Firstly, php returns the number of seconds since 1970/01/01, jquery returns a number of milliseconds, so there is no way to be the same value.
Second - even if you've got the fastest server in the world it comes to the milliseconds in the execution of lines of code. So exactly the same value can hardly be achieved :)
What you can do to try to trim jquery for the last three numbers representing the milliseconds (this of course if you do these two lines of code to execute in one second :))
And for last, there is a issue of clocks on your server and client computer - it must be exactly the same.
The javascript method getTime() returns microseconds (http://www.w3schools.com/jsref/jsref_gettime.asp) whereas PHP time() (or in your case strtotime() http://php.net/manual/en/function.strtotime.php) returns seconds. The first depends on your clients clock, the latter on your servers clock...
Mostly you never will get the same timestamps this way... maybe you could work around using some kind of AJAX api to have the same timestamp on both sides...
Check this :
In php:
echo strtotime(date('H:i:s')); // 1470294647
In Script :
var date = new Date();
var d = Date.parse("'"+date+"'")/1000; // 1470294647
alert(d);
I have a start and end time in milliseconds.
I have to get all the TV series that are ON AIR when the user visits the page.
So I am trying to do this:
if($prog["inizio"] < time() && $prog["fine"] > time()){
array_push($programmazioneFinal[$date."-".$prog["id_canale"]], $prog);
}
The logic is to get only those series whose starting time is lower than now (the serie is already started) and the end time is bigger than now.
For some reasons it is also returning those series that start much later in the day, not just the ones ON AIR now.
What's wrong?
I have added a screenshot of my DB just to make this clearer.
Thank you!
In PHP: You can use strtotime. This will give you timestamp value which you can use for comparison
strtotime("now")
Edit in your code
if($prog["inizio"] < strtotime("now") && $prog["fine"] > strtotime("now")){
array_push($programmazioneFinal[$date."-".$prog["id_canale"]], $prog);
}
Make your query look like
Select * from ... WHERE UNIX_TIME($your_date_parameter) BETWEEN inizio AND fine
With PHP, I am trying to convert a bunch of numbers into a a readable format, the thing is, I have no idea how/what format these are in or can be parsed in using the date() or time() functions in php. there are two of these as well.
(they're built from a total time spent online and time since last log-on)
onlinetime : 1544946 = 2w 3d 21h 9m
lastonline : 1397087222 = 1h 32m
does anyone know the way to get the two different times from the two different timestamps?
If you have a Unix timestamp, take a look at Convert timestamp to readable date/time PHP. The PHP documentation is here: http://php.net/manual/en/function.date.php.
For the online time, you could do modulo arithmetic to figure out the values for each, and then just make a string out of the result. Someone may have a nicer suggestion for this though.
I think John is right, the first is the number of seconds in the timespan listed. And the second certainly looks like a unix timestamp to me. So here's how you can get what you want from these sets of numbers:
1) For the first number, simply divide the number by the seconds in a given time span and use floor():
$timeElapsed = 154496; // in this case
$weeksElapsed = floor($timeElapsed / 604800);
$remainder = $timeElapsed % 604800;
$daysElapsed = floor($remainder / 86400);
etc...
2) For the second number, you can do the same thing by first getting the current timestamp and then subtracting the given timestamp from it:
$lastOnline = 1397087222; // again, in this case
$currentTimestamp = time();
$elapsedSinceLastLogin = $currentTimestamp - $lastonline;
$weeksSinceLastLogin = floor($elapsedSinceLastLogin / 604800);
etc...
I have a PHP file that randomly generates an image from a folder on every refresh. I downloaded it from here (which also has an explanation).
Instead of randomly choosing images, how can I have it change the image hourly? For example, I would like it have the same image for an hour, and then change when that hour is up. Basically, a new image based on some time interval.
Thanks for the help.
Find line
$imageNumber = time() % count($fileList);
And replace it with
$imageNumber = (date(z) * 24 + date(G)) % count($fileList);
That should work for you.
I'd say you need a random oracle function. Basically, it's a random() function that takes an input and generates a random number, with the guarantee that all calls with the same input will give the same output.
To create the value you pass into the oracle, use something that'll change hourly. I'd use julian_day_number * 24 + hour_number or something of that variety (just hour_number isn't good enough, as it'll repeat itself every 24 hours).
Then, whenever your page loads, generate your hour number, pass it through your oracle, and use the result just like you use your random value now. It'll still appear random, and it'll change once an hour.
Hope that helps!
Edit: Random oracles don't need to be fancy - they can be as simple as (stolen blatantly from this answer to a different question):
int getRand(int val)
{
//Not really random, but no one'll know the difference:
return ((val * 1103515245) + 12345) & 0x7fffffff;
}
Keeping it simple, put 8 different pics in img/ named from 1.jpg to 8.jpg, then:
$imagePath = sprintf("img/%s.jpg", (date('G') %8) +1);
with G param being:
24-hour format of an hour without leading zeros.
Now you are sure that you have a different pic every hour, and everybody sees the same.
EDIT: narrow or widen the repetition period adjusting modulo, 24 has a few divisors [1, 2, 3, 4, 6, 8, 12].
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