I want count down timer of $time value in php - php

The timer must stop when 24 hrs completed from the time of $time. It means when i recieve the value of $time. counting down till 24 hrs from then the timer must stop.
<?php
date_default_timezone_set("Africa/Lagos");
$time = `enter code here`date('H:i:s', time() - date('Z'));
echo $time;
?>

It's really better that you do this in JavaScript or jQuery. PHP isn't really meant to do stuff like that. However, if you want something to happen in 24 hours you could set up a cronjob. However, PHP is a preprocessor for HTML. It's supposed to help create the necessary HTML and have it sent back to the browser. The file will literally be loading for 24 hours before it finally stops loading to say timer complete. Also with modern browsers, it could time out.

Related

Find how many minutes old a file is

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

PHP working with milliseconds

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%

PHP Date() Function Minutes Behind

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!

Setting and stamping time on submit with php

I'm fairly green to PHP and I'm looking for a way to stamp time when a user, submits the form. I only want that time and not the current time. I have tried a few things like print, echo etc. But I can't seem to understand it properly.
Additionally to this my server time is returning a time that is an hour fast. I have tried to combat this with the following but no with no luck.
heres what I have;
// The following script only executes when the form is processed.
// set server timezone. declared at the top of my document.
date_default_timezone_set("Europe/London" . '-1');
// set date & time. placed within my isset submit argument.
$date = date ("l, F jS, Y");
$time = date ("h:i A");
I'm currently calling the these variables with echo and EOD but, this is always giving me the server time even if the page is refreshed. I'm looking for a stamp of the time.
I'm echoing these variables out within an EOD and as a result, the print. command is being converted to simple text.
would anyone know of a quick fix to this or am I being a little too green?
In your script, try just:
date_default_timezone_set("Europe/London");
If that is the timezone you want your script to use.
Then on your server, make sure the server is set to use UTC (Coordinated Universal Time). That is a server set up issue.
https://www.devside.net/wamp-server/setting-the-default-timezone-for-php-to-use

How to supply a value to the javascript method Date.setTime() from PHP?

Is it time()?
Is it time().substr(microtime(), 2, 2)?
Is it time().substr(microtime(), 2, 3)?
Kind of lost with the following snippet.
function updateClock ( ) {
var timeStamp = <?php echo time().substr(microtime(), 2, 2);?>;
var currentTime = new Date ( );
currentTime.setTime( timeStamp );
...
...
}
My goal is to use server time and start ticking from there on client browser window. The code above either returns the current client computer time or sometime in 1973. I guess I'm not getting the right time stamp format for setTime()?
Thanks!
1000
I tried that but the web page still shows my local time after I upload the js.php (rendering the javascript code) to my server. My server is approx 12 hours different in time from me. My guess is that does php takes client side time into account running time() ? I mean browsers do send request time to apache right?
I copied the time() * 1000 returned value from the web page run on my server and pasted it into a local page:
<script type="text/javascript">
var d = new Date();
d.setTime(1233760568000);
document.write(d);
</script>
And it's indeed my local time. Thus the guess.
Is there anyway to specify time zone for time()?
Date.setTime expects the number of milliseconds since 1970-1-1. php's time function yields the number of seconds since 1970-1-1. Therefore, you can just use
var TimeStamp = <?php echo time()*1000;?>
Due to latency issues (the browsers needs to load the whole page before starting JavaScript), the time will usually drift one or a couple of seconds though.
Multiply by 1000. JavaScript expects milliseconds while PHP returns seconds.
Date.setTime() wants milliseconds since the Unix Epoch, and time() returns seconds since then. If absolute precision isn't required (and given your methodology, I don't think it is), just multiply the value you get from time() by 1000.
Edit: beaten twice--D'oh

Categories