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));
Related
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%
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 am trying to connect to the server every minute and pass in the variable of the current time, which would be the lastUpdate. On the server side php file, I plan to compare the passed in time variable with a TIMESTAMP in the database rows; much like if (time1-time2 > certain value)... So far the javascript code is:
var time;
var timer_is_on = 0;
localStorage.lastUpdate = 0;
localStorage.numUpdates = 0;
function timedCount()
{
localStorage.lastUpdate = new Date();
//connect to server
contactServer(localStorage.lastUpdate);
currentCount=currentCount+1;
time=setTimeout("timedCount()",60000);
}
My question is whether I am doing this correctly. I am declaring localStorage.lastUpdate as new Date() and I'm not sure whether this is correct? I have tried the loop and every minute lastUpdate seems to be the same date and time.
My last question is whether I can actually compare the two time formats from javascript and php. In the SQL timestamp, the format is 2012-03-20 11:14:40 while the date format from the javascript new Date() is Tue Mar 20 2012 12:32:44 GMT-0400 (Eastern Daylight Time).
Any information would be helpful, thanks!
Be careful when you use time. Your server time may be different from your client time. The best for you is to store the date of the last request in $_SESSION['last'].
With this solution, all your problems are not anymore. Your PHP set the date, so it is the same reference, and it's in a good format.
Just keep your timeout on the client side:-)
Use new Date().getTime() to return a UNIX timestamp; that is, the number of seconds since Jan 1st, 1970. This makes it much easier to compare with other times.
localStorage.lastUpdate = new Date().getTime();
Will return something like this: 1332266002.
The PHP equivalent is simply time().
And to convert that UNIX timestmap into a MySQL TIMESTAMP, use MySQL's FROM_UNIXTIME function.
I'm failing a bit to get this working.
I read out a date from my database in the yyyy-mm-dd format. However, I need to use it in my jQuery ajax call in the dd-mm-yyyy format. Is there a way to turn my dates around? I should've seen this coming when I started working on my app, but alas, I didn't :(
Don't feel like changing around the way I save stuff to my DB so I was wondering if anyone knows an easy way to change the format? Thanks :(
EDIT: Just ran into another, similar problem
I read time out as, for example, 08:00:00 I want to split this into parts aswell. For example
08:00:00 => var a = 8, var b = 00 // ignore seconds
09:30:00 => var a = 9, var b = 30
23:45:00 => var a = 23, var b = 45
10:30:00 => var a = 10, var b = 30
:( Thanks!
Format your date directly in your sql query :
SELECT DATE_FORMAT(fielddate,'%d-%m-%Y') as jsDate, DATE_FORMAT(fielddate,'%m-%d-%Y') as phpdate FROM xxx
You can do multiple format in the same query to fit your need (a format for js , one for php, one for direct display ...)
See date and time function
In PHP you can easily turn it around.
$date=date('d-m-Y', strtotime('2009-11-12'));
You could also achieve this using Javascript:
var date='2009-11-12'.split('-').reverse().join('-');
jsFiddle Demo
EDIT concerning your update:
var timeArray=myTime.split(':'); is what you need here. It grabs a string, and returns a normal array with the elements of the string splitted by :. So timeArray[0] will be the hour, timeArray[1] the minute.
Yes, you can turn it around, but yyyy-mm-dd is the internationally accepted way to represent dates in computers, so you really should not.
Instead, you should change your database, and if you want to present the date to the user in another format, you do the conversion for the presentation only.
EDIT: Sorry if this answer sounds rude, but I really believe that you will thank me later if you do this. Or at least keep it in mind until next time :)
use date function:
date("d-m-Y",strtotime($yourdate));
<?php
$newdate = date('d-m-Y',strtotime($db_date))
?>
Try this: jquery-dateFormat Plugin for jQuery
The server my PHP script is running on is set to UTC. I can't seem to figure out a way to set all dates to the browser's timezone. All dates displayed are formatted using PHP's date() function.
I know that via JavaScript's getTimezoneOffset() function, I can get the browser's current UTC offset (-4, in my case). How can I tell PHP to use this offset? I can use date_default_timezone_set(), but how do I convert an offset, say -4, to a time zone string, say America/New_York?
Note: The server is running PHP 5.1.6 (with no DateTime class) and I am using CodeIgniter.
As per the comment above- you could use a cookie...:
Javascript:
var today = new Date();
function SetCookie(cookieName,cookieValue,nDays) {
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}
SetCookie("datecookie", today, 30);
window.location.href=window.location.href
PHP:
echo date("m d Y", strtotime($_COOKIE['datecookie']));
Because Javascript is client based and PHP is server based, they will not communicate. To tell the server the browser time, you will need to send information from the client, perhaps via Ajax, posting the browser time. The target PHP script should then handle/output this as appropriate. One other (obtuse) option is to attempt to geolocate the user based on IP, then guesstimate the correct time for that location- however this is inefficient for this purpose.
"I know I can use date_default_timezone_set(), but how do I convert an offset, say -4, to a time zone string, say America/New_York?"
Simply look at the comments associated with the documentation for that function. Comment #99006 does the work for you. You just need to get the timezone offset from the user.