Countdown based on MySQL entry - php

I have a database that logs the date and time a song was played in a format like 2011-04-13 17:55:46. It also logs the length of the song in milliseconds. How would I make a live countdown for the user to see as well as a refresh countdown?

For Javascript, you can use the Date() object created from a string with the given time (you can print it to the screen in a hidden input, span, or whatever). I assume the end time is when the song ends. You can calculate this very simply based on the difference between the play time and the song length. When you say "refresh countdown," do you mean if they refresh the page? Well, I'm not sure how the song would keep playing at the given time, but you could check that the song was started and use the time you have above and do your calculation again.

Related

want to make changes with countdown in php

i want to make some changes for my site,but i don't know how;
and i will be glad if you could help me with that,
if its possible to see really online the seconds counting until zero,not only the time has left
and second, if i want that the countdown will start automatically to count again and start from the begining by a loop.
here is the code that I want to insert my changes
<?php
//You must call the function session_start() before
//you attempt to work with sessions in PHP!
session_start();
//Check to see if our countdown session
//variable has been initialized.
if(!isset($_SESSION['countdown'])){
//Set the countdown to 120 seconds.
$_SESSION['countdown'] = 120;
//Store the timestamp of when the countdown began.
$_SESSION['time_started'] = time();
}
//Get the current timestamp.
$now = time();
//Calculate how many seconds have passed since
//the countdown began.
$timeSince = $now - $_SESSION['time_started'];
//How many seconds are remaining?
$remainingSeconds = abs($_SESSION['countdown'] - $timeSince);
//Print out the countdown.
echo "There are $remainingSeconds seconds remaining.";
//Check if the countdown has finished.
if($remainingSeconds < 1){
//Finished! Do something.
}
​
PHP is a server side scripting language and it stands for Preprocessor Hyper Text (Personal Home Page before that).
What PHP does is not what you want to do with it if you catch my drift.
PHP takes dynamic code e.g. records from a database and generates the HTML that goes with it (often you have to create loops for products etc...)
When PHP is done it sends the response to the browser and is done processing from there.
You want to add a counter that works live on the page itself.
This is a different story.
You'll need to write some JS (JavaScript) on page load that will count down from where the counter was when the page first loaded.
You could make a script in JS that takes a given timestamp and counts down from there.
Each time you load a new page with the JS doing its thing onload will make sure the counter will count down until it hits 0.
I would love to help you further but I'm # work at this moment so all I can do is point you into the right direction.
What I think you want to do:
get timestamp from PHP and insert it into a div
load JS on pageload
fetch the hidden div timestamp
countdown dynamicly from the timestamp using js
And one last thing (seeing as you don't seem to have alot of experience with php / js) - instead of using jQuery, further complicating things for a very small puny task I would look up a plain JS example of what you are trying to do and learn from there.
using jQuery for just this is waaaaay overkill and not needed at all - adding 90kb code for a silly clock that is ;)

Change RSS feed, but only new items

I'm fairly new to PHP, and I'm trying to write a script that solves the following
I have an RSS feed that gets saved to my server every 10 minutes (copied from elsewhere).
There is a problem with the timestamps (pubDate tag) on the RSS feed, they always have the correct date but 00:00:00 GMT as the timestamp (I have no control over this).
Therefor, when I use an autotweeting program to tweet updates from the feed (it checks it every hour or so), it won't - It only tweets the first update of each day as a result.
Therefor, what I'm trying to do to fix it to some degree is to check if the feed has changed, and if it has, change the saved pubDate to the current server time on only the new items.
I'm also kind of confused as to how I can have it check for changes - If I have a corrected version (with fairly accurate timestamps) saved to my server, it will ALWAYS think there are changes, because the timestamps will always be 00:00:00. I'm thinking, check both feeds for items including the full strings such as <guid isPermaLink="true">http://services.runescape.com/m=adventurers-log/a=161/display_player_profile.ws?searchName=A13d&id=-463827091</guid> - Since the id= at the end stays constant, it would only change the <pubDate> of items found to be new.
http://services.runescape.com/m=adventurers-log/a=161/rssfeed?searchName=A13d Here is a feed as an example. If anyone could get me started or point me to some kind of tutorial that might help, I'd really appreciate it. This is over my head, but something I need to learn how to do.
Maybe there is something wrong with your code parsing the timestamp, date format perhaps?
I believe the method of doing full string comparisons(<title> & <description>) between items with the same <guid> is your best bet. Here is some reading about RSS duplicate detection if you are interested.

Information stored in a cookie file

Thanks for you help in advance.
I am trying to figure out the structure of the cookie file, more specifically i want to be able to determine the expiry time. From the cookies i have created they all appear to be in a standard format. Name, Value, website,followed by 5 numbers and a star. See example below.
name
value
www.website.co.uk/
1536
3041141504
30135951
1632526096
30135949
*
Obviously the expiry time is one of the numbers, the question is which one. From experiments I have determined that the first and fifth number don't seem to change. In a case where i generated three cookies at the same time with a 1000 second time difference i noticed that the fourth number appeared to increase by 2000 suggesting that this has a connection with the expiry time.
Can anyone confirm if i am heading in the right direction? And does any one know how i convert this to a human time and date(preferably in php but any language would give me a starting point)
thanks
Jason
I am trying to figure out the structure of the cookie file...
There isn't one. It's entirely up to the browser where and how to store cookies on the client computer. You can certainly solve this for popular browsers, but it won't be the same solution for all of them. Some store cookies in flat files; others store them in individual files (per cookie, or per site), others as SQLite databases...
I have figure out the answer. Thanks to everyone who responded.
The format of the the cookie in ie8 is:
name
value
website
flags-not entirely sure what this is
expiration time (low)
expiration time (high)
creation time (low)
creation time (high)
To convert these numbers into actual times you can use the following formula:
time = 1*10-7*(high*2^32+low)-1164447360
thanks again
Jason

HTML form input to mysql TIME

I am having an add/edit form to update and add to database, and I was not sure what the best way is to input TIME type (HH:MM:SS). Should I use multiple html text inputs for HH, MM, SS?
if so, is there a function that prepares the string for database input?
Basically what I'm trying to input is how many hours, minutes, seconds a specific task took to finish.
Can anyone point me in the right direction here?
I'm designing a website using Codeigniter (PHP).
Thanks
Let me go ahead and clarify what needs to happen a bit more...
The user is required to enter data specific to sports more in particular to a players minutes and seconds played. I'm thinking of maybe simplifying it to only minutes. Perhaps this way input is only 1 thing. Then again my question is, what method would work to convert this "minute number" to the correct MYSQL TIME format?
I wrote a helper to do something similar in an app I'm working on. Mine generates three dropdowns, hh, mm and am/pm, by calling built in form_dropdown helper. Once I get the data from the drop downs, I convert it 24hr format and then I just concatenate the strings into the right format for MySQL. Since it's a helper I can just call it from any view using form_time(). I can post it here if you think it would help to see it.
Dana
just use now() function, if your DB is
MYSQL is you want to save the current
time
e.g:
UPDATE tbl SET timemodified = NOW()
and make sure that timemodified has a
type of "time"
Ow sory miss that point. uhm maybe you need to have a start time in your DB, then after he/.she is finish his/her task. you must query on the DB the start time subtract it to the end time(your current time) then the result would be the time he/she performed the task
this checks for 2 numbers, then a ":" then 2 numbers, then a ":", then finally 2 numbers again:
$cleanTime = preg_match( '/(\d\d)\:(\d\d)\:(\d\d)/', $_POST[ 'NAME_OF_TIME_INPUT' ] );
if( !$cleanTime ){ /* ... error ... */ }
don't be scared of all the slashes, haha (I was at first when I used regexps).
replace NAME_OF_TIME_INPUT with the content of the name attribute on the <input on the <form page
i.e. if <input name="coolinput" /> then use $_POST[ 'coolinput' ]
This is one of the eternal struggles of (web) UI design, how to input time without driving the users nuts. What works for your specific case is something only you can decide, because it depends on the exact format/circumstances you need and your target audience.
As general guidelines I'd say:
Don't do a free-form text field that requires a certain format, e.g. "Enter time (HH:MM:SS)", because it's too easy to mess up and will deny the users input or mess up the time if you do no validation.
Try to avoid [0-23] [0-59] [0-59] dropdowns, since they can be quite a pain (click, scroll, click, click, scroll, click, click, scroll, click).
If ease of use is a high priority, as would be the case for public websites, maybe a Javascript enhanced timepicker is a good idea. Try not to use anything too fancy that nobody gets though (like dragging the hands on a clock).
A free-form, free-format text field might be the best idea. The user can just type in "3pm", "16:34" or "midnight". You may need to provide examples to get users started, otherwise they may feel lost. You can run this through strtotime on your end, but you may need to fill in the blanks and do a lot of validation.
Three short text fields may be a good idea if your audience is very keyboard focused and can be expected to tab through them in rapid order.
As for formatting it for SQL, however you receive the time input from the user, you should assemble it to a UNIX timestamp and format that timestamp for SQL:
date('Y-m-d H:i:s', $timestamp);
I have searched for some alternatives and solutions and I came up with this:
$min = 60;
$time[] = floor($min/60);
$time[] = $min%60;
And I used the following to convert to MySQL TIME format
INSERT INTO table (min) VALUES (MAKETIME($time[0], $time[1], 0))
I have had the same issue with working with the html time input. However, I've managed to work around it with a PHP function.
What the function does is translate the time into a format the MYSQL DATETIME datatype can understand.
Of course you will need to enter the date somehow, but I'll leave that up to you.
functions.php
function convertHtmlTime($date,$time){
$newDate = date($date);
$newTime = date($time);
$datetime = new DateTime($newDate.$newTime);
return date_format($datetime, 'YmdHis');
}
test.php
$date="2007-02-16";
$time="23:59";
echo convertHtmlTime($date,$time);
Results:
20070216235900

display time in a web page

I have page where where I list all comments for a post. Next to each comment I have a time value in full format including the date/time (2010-01-02 11:11:20).
I know that I can format it in PHP before displaying it, so it shows;
posted 40 secs ago
posted 5 days ago
but that would not be efficient as I am going to cache the page once it is generated.
On SO I see that they have some kind of java script for showing it, so it is run on the browser of the client:
alt text http://img528.imageshack.us/img528/5442/35118769.png
So basically I need a java script that runs on the browser that would instead of showing my date/time show "posted 4 hours ago". All help is welcome.
Sounds you want this: timeago: a jQuery plugin.
Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
I quote further:
Avoid timestamps dated "1 minute ago" even though the page was opened 10 minutes ago; timeago refreshes automatically.
You can take full advantage of page caching in your web applications, because the timestamps aren't calculated on the server.
So it should fit your needs.
You can put the "timestamp" of the page into the page when you render it. This is the fixed time that doesn't change. Look at PHP's time function for this (http://www.php.net/manual/en/function.time.php). It gives you seconds since the UNIX epoch started.
Then, when the page is running, use Javascript's Date object. It encapsulates something similar. Do a getTime() on a new Javascript Date object, which will get you also seconds since the UNIX epoch.
Subtract one from the other to get the elapsed time between then and now, and do whatever pretty formatting you need to make it display right in your page.
Edit: See also Felix's answer about timeago, which is a nifty tool to do the second half of this process for you automatically.
Output your date in a format accepted by the parseDate method or as a constructor parameter for date. Place them in some nodes which can later on easily be grabbed by a javascript framework (or your own methods) and then perform some time/datediff methods on these grabbed values and replace the node's content.

Categories