I have the following value (generated as GMT Time) being retrieved from an API call
Song Started Time : 2017-09-06T16:51:02.000Z
I also have the duration (in form of milliseconds) of a specific song tied to that record in the API response. For example, it may return:
222000
Then, using PHP GMT time function I'm checking what the current time is on a PHP page.
World Current Time: 2017-09-06T16:51:31.000Z
Using PHP, how would I be able to determine how far along in the song I currently am, using the start time, fixed duration of the song, and the current time. I figure this should be fairly simple, but I'm struggling to figure out how to add milliseconds in PHP. Ideally, the output I'm looking for should just say .33 to indicate the song is currently 33% completed.
So you need to find song played time in percent
<?php
$date = new DateTime("2017-09-06T16:51:02.000Z");
$date2 = new DateTime("2017-09-06T16:51:31.000Z");
$interval = $date2->diff($date)->s;
$duration = 222;//222000/1000 to make milliseconds in seconds
echo $song_played = (int)(($interval/$duration)*100) . "%";
?>
Live demo : https://eval.in/856523
Example you gave is for 13% not for 33%
Related
First of all, I know I am not supposed to as "code sample" here. Sadly, I am not a programmer and I have a situation where I need to update a line in a report to present to the customer, but I do not know how to do it.
I have access to PHP file report.php. In the same server and folder as report.php there is a file called report.csv. When report.php is loaded in browser, I want to show one line which will say:
Report.cvs is X minutes old that is all.
If the report is 10 days old, then also I can show the age in minutes. I dont need any complicated X days, Y hours, Z mins etc.
I am worried i might break something in server if I try to add myself since I am not programmer. Is there anyone who can show me what I need to add to report.php to make this work?
Looking through the documentation http://php.net/manual/en/function.fstat.php you might find that filectime ( string $filename ) may be useful.
Now if the file is consistently updated by users you may find that storing the creation/upload time in a Database like SQL/sqlite may be useful.
Ok so basically you need to get the time when the file was last modified, subtract it from the current time, convert the result to minutes and voilà.
Here's what that should look like:
$file = ''; // --- Path to the file
$filetime = filemtime($file); // --- File time
$now = time(); // --- Current Time
$fileAge = round(($now - $filetime)/60); // --- Subtract file time from current time, divide by 60 for the minutes and round the result.
echo basename($file).' is '.$fileAge.' minutes old.';
I have an odd situation, I am using the code below:
$currentTime = date("YmdHis");
echo 'Current Time = '.$currentTime;
echo '<br>';
When running this, sometimes it is running around 7 minutes too slow.. example:
Image - Actual Time 11:35, PHP Current Time 11:27
But then sometimes, it runs perfectly.. example:
Image - Actual Time 11:37, PHP Current time 11:37
Is there any reason why this is happening? I am basing calculations on the current time which is obviously causing problems when it is slow!
I've researched this a lot but can't find a satisfactory answer; how do I create a PHP script that will generate a new number each day? Obviously I'm using this for a reason other than to generate a random number daily, but I won't go into that reason, it'll just make this question more complicated. So I'm asking this: How do I generate a random number which will change each day in PHP? Using MySQL will be no problem, but it must be automatic so I won't have to manually change it daily. (Here's my 'script' to generate a random number)
<?php
echo rand(1,100)
?>
Any answers are appreciated, Thanks
- Hugh
Use time() function to generate seed, then use regular rand.
This way you should't to store it anywhere and you always can regenerate it when needed.
function randomEveryDay()
{
$now = time();
$today = $now - ($now % 86400); //86400 = 1 day in seconds
srand($today);
return rand();
}
Or more interesting example without random at all.
function randomEveryDay() {
$now = time();
$today = $now - ($now % 86400);
$hash = sha1('salt string'.$today);
return intval('0x' . substr($hash, 6, 8));
}
Every day you will get the same number in $today, then use any cryptographic\non cryptographic hash function to generate "random".
This is fairly a simple task as long as you understand the basics of crontabs.
Step One: Create the script. This is basically going to be what creates the "text" then inputs it into the database via mysqli. For example, if we are generating a random number, what you have so far is good, you will just need to insert it into a database table. I recommend using a time stamp to give what day it was generated on
Step Two: Create a cronjob. Use the servers crontab to run a task every day, this can be done by adding this to the cron file: This will run a cron each new day.
00 01 * * * php path/to/your/generate.php
Step Three Fetch result from database by using the current date. If you are needing to display that text, pull it from the database using whatever the current day is from date() or DateTime
It's impossible to give a good answer without knowing exactly what you want to do, so this will generate a new number every day:
echo date('Ymd');
I have checked out the following question/responses:
How do I get the format of “yyyy-MM-ddTHH:mm:ss.fffZ” in php?
The responses include links to Microsoft documentation to format dates but these do not work in PHP.
The the top answer suggest
date('Y-m-dTH:i:s.uZ') //for the current time
This outputs
2013-03-22EDT12:56:35.000000-1440016
Background
I am working with an API which requires a timestamp in the format above. The API is based in the UK (GMT) and my server is in Australia (AEST).
The example given in the API documentation ask for the date to be in this format:
2011-07-15T16:10:45.555Z
The closest I can get to this is date('c') which outputs:
2014-07-03T16:41:59+10:00//Notice the Z is replaced with a time diff in hours
I believe the 'Z' refers to a Zone but it is not mentioned in the PHP documentation.
Unfortunatly when I post this format, the API is reading the time and taking 10 hours off. I get an error saying that the date cannot be in the past (as it is checking against the local time in Melbourne, but seeing a time 10 hours earlier).
I have tried trimming the timestamp to remove the +1000 which the API accepts, but the record is showing as created as 10 hours earlier.
I need to match the timestamp required but I cannot find any way to replicate the above output, in PHP for Melbourne, Australia. Any assistance is much appreciated.
First question on SO so please let me know how I have gone
Z stands for the timezone UTC and is defined in ISO-8601, which is your desired output format, extended by the millisecond part.
Before outputting the time, you'll need to transfer local times to UTC:
$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone('UTC'));
then you can use the following format string:
echo $d->format('Y-m-d\TH-i-s.\0\0\0\Z');
Note that I've zeroed the millisecond part and escaped the special characters T and Z in the format pattern.
The 3 number last before Z is just the 3 decimal place of time in milliseconds
The function microtime(true) gave the current time in milliseconds, would output like 1631882476.298437
In this situation it would be .298Z
Examples
1652030212.6311 = .631Z
1652030348.0262 = .026Z
1652030378.5458 = .545Z
Codes
$milliseconds = microtime(true);
// Round to integer
$timestamp = floor($milliseconds);
// Get number after dots
$uuuu = preg_replace("/\d+\./", "", "$milliseconds");
// Get last 3 number decimal place
$u = substr($uuuu, 0, 3);
// Print date by the timestamp timestamp
echo date("Y-m-d\TH:i:s", $timestamp). ".{$u}Z";
I want to show an ActionScript 3.0 Timer, with respect to a timestamp gotten from MySQL.
So suppose MySQL (PHP) returns a string like $my_birthday="2013-05-22 12:30:45"
I got it from ActionScript by a URLLoader, and I want to show the timer like this:
My age:
01 hours 01 minutes 42 seconds
What function should I use in:
public function timerHandler(e:TimerEvent)
{
log_textfield.text = String((new Date()).time) - my_birthday; // I need to convert the Date().Time to unix timestamp I guess, and a function for time difference.
}
This answer has a TimeSpan class that you may want to use. The code below should do what you need to get the TimeSpan where you can get the parts you need to display. I don't have Flash on this computer though to test, so your mileage may vary :)
// new Date() is allergic to dashes, it needs slashes.
my_birthday = my_birthday.replace('-', '/');
var sinceBirthday = TimeSpan.fromDates(new Date(), new Date(my_birthday));