|
-
Unix Timestamp Conversion in AS1
I am using Flash CS4, but AS1 (I know I should be in AS2 at least, but this particular code, I didn't know how to accomplish in that yet).
I created a widget that gets an array of information from my MySQL database and it is user specific. Everything works, except one of the fields is a date and is in Unix Timestamp format. How can I convert the timestamp in AS1 to display as a regularly formatted date? (without specifying a specific timestamp in the as, because it is a changing variable).
Thanks!
-
var x:Number = 1; x /= 0;
Well, in Unix time, each day is represented by 86,400 seconds. 0 in Unix time represents Thurs, 1 Jan 1970 00:00:00. From that you can write an algorithm to calculate the date.
Last edited by ZippyDee; 01-02-2010 at 03:36 PM.
-
Sorry I'm a newbie and have no idea what that algorithm would look like. I was kinda hoping there would be a simple as code to convert it. Any suggestions? Thanks.
-
var x:Number = 1; x /= 0;
I wrote a quick function to do it. It's not very efficient, but I'm working on a better one. This one gets the job done:
Code:
//the variable "utime" is the unix timestamp number
var days=Math.floor(utime/86400);
var daynames=new Array("Thurs","Fri","Sat","Sun","Mon","Tues","Wed");
var dayOfTheWeek=daynames[days%7];
var temp=utime-days*86400;
var hour=Math.floor(temp/3600);
var minute=Math.floor((temp-hour*3600)/60);
var second=temp-hour*3600-minute*60;
var year=1970;
temp=days;
var yrlen=365;
while(temp>=yrlen){
year++;
temp-=yrlen;
yrlen=365+(year%4==0 && (year%100!=0 || year%400==0));
}
var mlen=new Array(31,28+(yrlen==366),31,30,31,30,31,31,30,31,30,31);
var month=0;
while(temp>=mlen[month]){
temp-=mlen[month];
month++;
}
var monthnames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var monthName=monthnames[month];
month+=1;
var day=temp+1;
trace(dayOfTheWeek+", "+monthName+" "+day+", "+year);
trace(month+"/"+day+"/"+substring(String(year),3,2));
trace(hour+":"+minute+":"+second);
The the trace functions show how to use the results.
dayOfTheWeek -- the day as a string ("Thurs", "Mon", "Fri", etc.)
day -- the day as a number
monthName -- the month as a string ("Jan", "Aug", "Dec", etc.)
month -- the month as a number
year -- the year (obviously as a number)
hour -- the hour (24hr time, not 12 hr)
minute -- the minute
second -- the second
Hope that works for you!
-Zippy Dee
//EDIT: I didn't code this to work in negatives, so it only works for dates past 1970. I don't think that will be a problem for you, but I thought I'd let you know anyway.
-
Other option
I believe that, since you get your data from MySQL, you use PHP.
PHP already has a function for that:
DateTime::setTimestamp — Sets the date and time based on an Unix timestamp.
gparis
-
thanks...
 Originally Posted by ZippyDee
I wrote a quick function to do it. It's not very efficient, but I'm working on a better one. This one gets the job done:
Code:
//the variable "utime" is the unix timestamp number
var days=Math.floor(utime/86400);
var daynames=new Array("Thurs","Fri","Sat","Sun","Mon","Tues","Wed");
var dayOfTheWeek=daynames[days%7];
var temp=utime-days*86400;
var hour=Math.floor(temp/3600);
var minute=Math.floor((temp-hour*3600)/60);
var second=temp-hour*3600-minute*60;
var year=1970;
temp=days;
var yrlen=365;
while(temp>=yrlen){
year++;
temp-=yrlen;
yrlen=365+(year%4==0 && (year%100!=0 || year%400==0));
}
var mlen=new Array(31,28+(yrlen==366),31,30,31,30,31,31,30,31,30,31);
var month=0;
while(temp>=mlen[month]){
temp-=mlen[month];
month++;
}
var monthnames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var monthName=monthnames[month];
month+=1;
var day=temp+1;
trace(dayOfTheWeek+", "+monthName+" "+day+", "+year);
trace(month+"/"+day+"/"+substring(String(year),3,2));
trace(hour+":"+minute+":"+second);
The the trace functions show how to use the results.
dayOfTheWeek -- the day as a string ("Thurs", "Mon", "Fri", etc.)
day -- the day as a number
monthName -- the month as a string ("Jan", "Aug", "Dec", etc.)
month -- the month as a number
year -- the year (obviously as a number)
hour -- the hour (24hr time, not 12 hr)
minute -- the minute
second -- the second
Hope that works for you!
-Zippy Dee
//EDIT: I didn't code this to work in negatives, so it only works for dates past 1970. I don't think that will be a problem for you, but I thought I'd let you know anyway.
That's great, thanks so much for taking the time to write out the code for me! Unfortunately, when I run it I get "A script in this movie is causing Flash Player to run slowly. If it continues to run, your computer may become unresponsive. Do you want to abort the script?"
Here is my full AS code, including what I added from you:
Code:
System.useCodepage = true;
format = new TextFormat();
desc.html = true;
header.text = "Loading...";
date.text = "Loading...";
loc.text = "Loading...";
desc.text = "Loading events...";
var userName = _root.userName;
widgetname.text = userName + "'s Event Schedule";
temp = new LoadVars();
temp.load("http://www.theband-aid.com/getdata.php?user=" + userName);
temp.onLoad = function(){
myArray = new Array();
for(var a in this){
if(a != "onLoad"){
myArray.push(this[a]);
}
}
header.htmlText = myArray[0];
date.text = myArray[1];
loc.text = myArray[2];
desc.text = myArray[3];
var oldt = getTimer();
var ok = true;
var l = myArray.length;
var i = 4;
_root.onEnterFrame = function(){
t = getTimer();
if(t-oldt>15000){
header.htmlText = myArray[i];
date.text = myArray[i+1];
loc.text = myArray[i+2];
desc.text = myArray[i+3];
if(i<(l-4)){
i += 4;
}else{
i = 0;
}
oldt = getTimer();
}
};
};
//the variable "date.text" is the unix timestamp number
var days=Math.floor(date.text/86400);
var daynames=new Array("Thurs","Fri","Sat","Sun","Mon","Tues","Wed");
var dayOfTheWeek=daynames[days%7];
var temp=date.text-days*86400;
var hour=Math.floor(temp/3600);
var minute=Math.floor((temp-hour*3600)/60);
var second=temp-hour*3600-minute*60;
var year=1970;
temp=days;
var yrlen=365;
while(temp>=yrlen){
year++;
temp-=yrlen;
yrlen=365+(year%4==0 && (year%100!=0 || year%400==0));
}
var mlen=new Array(31,28+(yrlen==366),31,30,31,30,31,31,30,31,30,31);
var month=0;
while(temp>=mlen[month]){
temp-=mlen[month];
month++;
}
var monthnames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var monthName=monthnames[month];
month+=1;
var day=temp+1;
trace(dayOfTheWeek+", "+monthName+" "+day+", "+year);
trace(month+"/"+day+"/"+substring(String(year),3,2));
trace(hour+":"+minute+":"+second);
Thanks again for the help! I really appreciate it.
-
 Originally Posted by gparis
I believe that, since you get your data from MySQL, you use PHP.
PHP already has a function for that:
DateTime::setTimestamp — Sets the date and time based on an Unix timestamp.
gparis
I am using PHP. My problem is, I've tried a function before and it works for the first array, but then cannot recall the function after that. My output for the php looks something like this:
&desc0=this&loc0=here&date0=1261724400&title0=Even t&desc1=next one, etc.
Here is my PHP script:
Code:
<?php
/* $Id: profile.php 42 2009-01-29 04:55:14Z john $ */
$page = "getdata";
include "header.php";
$sql = "SELECT `se_events`.*"
. " FROM se_events"
. " WHERE event_user_id={$owner->user_info['user_id']}";
$result = mysql_query($sql) or die ("Error with query");
$num_rows = mysql_num_rows($result);
for($i=0;$i<$num_rows;$i++){
$row = mysql_fetch_array($result);
$title = "title$i";
$title = $row['event_title'];
$date = "date$i";
$date = $row['event_date_start'];
$loc = "loc$i";
$loc = $row['event_location'];
$desc = "desc$i";
$desc = $row['event_desc'];
print("&desc$i=$desc");
print("&loc$i=$loc");
print("&date$i=$date");
print("&title$i=$title");
}
// ASSIGN VARIABLES AND INCLUDE FOOTER
$smarty->assign('actions', $actions->actions_display(0, $setting['setting_actions_actionsonprofile'], "se_actions.action_user_id='{$owner->user_info['user_id']}'"));
include "footer.php";
?>
-
var x:Number = 1; x /= 0;
The problem is that you are calling the code to convert date.text before your loadVars has loaded. So it's trying to calculate with date.txt set to "Loading..."
You need to make a function to call the code I gave you.
just make it something like
Code:
function convertUnix(utime:Number){
//the code I gave you
return {dayOfTheWeek:dayOfTheWeek,day:day,monthName:monthName,month:month,year:year,hour:hour,minute:minute,second:second};
}
that makes it a function that returns an object with each variable as a property. So you could then say something like:
Code:
var ob:Object=convertUnix(date.text);
date.txt=ob.month+"/"+ob.day+"/"+substring(String(ob.year),3,2);
-
 Originally Posted by ZippyDee
The problem is that you are calling the code to convert date.text before your loadVars has loaded. So it's trying to calculate with date.txt set to "Loading..."
You need to make a function to call the code I gave you.
just make it something like
Code:
function convertUnix(utime:Number){
//the code I gave you
return {dayOfTheWeek:dayOfTheWeek,day:day,monthName:monthName,month:month,year:year,hour:hour,minute:minute,second:second};
}
that makes it a function that returns an object with each variable as a property. So you could then say something like:
Code:
var ob:Object=convertUnix(date.text);
date.txt=ob.month+"/"+ob.day+"/"+substring(String(ob.year),3,2);
Okay, I'm sure to get on your nerves, but I really appreciate your help and it's almost there!
Using what you gave me I am able to get the correct date to trace in the output window, however it does not show up in my desired dynamic text field.
Here is the complete AS code:
Code:
System.useCodepage = true;
format = new TextFormat();
desc.html = true;
header.text = "Loading...";
date.text = "Loading...";
loc.text = "Loading...";
desc.text = "Loading events...";
var userName = _root.userName;
widgetname.text = userName + "'s Event Schedule";
temp = new LoadVars();
temp.load("http://www.theband-aid.com/getdata.php?user=Social");
temp.onLoad = function(){
myArray = new Array();
for(var a in this){
if(a != "onLoad"){
myArray.push(this[a]);
}
}
function convertUnix(myArray:Number){
//the variable "date.text" is the unix timestamp number
var days=Math.floor(myArray[1]/86400);
var daynames=new Array("Thurs","Fri","Sat","Sun","Mon","Tues","Wed");
var dayOfTheWeek=daynames[days%7];
var temp=myArray[1]-days*86400;
var hour=Math.floor(temp/3600);
var minute=Math.floor((temp-hour*3600)/60);
var second=temp-hour*3600-minute*60;
var year=1970;
temp=days;
var yrlen=365;
while(temp>=yrlen){
year++;
temp-=yrlen;
yrlen=365+(year%4==0 && (year%100!=0 || year%400==0));
}
var mlen=new Array(31,28+(yrlen==366),31,30,31,30,31,31,30,31,30,31);
var month=0;
while(temp>=mlen[month]){
temp-=mlen[month];
month++;
}
var monthnames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var monthName=monthnames[month];
month+=1;
var day=temp+1;
trace(dayOfTheWeek+", "+monthName+" "+day+", "+year);
trace(month+"/"+day+"/"+substring(String(year),3,2));
trace(hour+":"+minute+":"+second);
return {dayOfTheWeek:dayOfTheWeek,day:day,monthName:monthName,month:month,year:year,hour:hour,minute:minute,second:second};
}
header.htmlText = myArray[0]
var ob:Object=convertUnix(date.text);
date.text=(ob.month+"/"+ob.day+"/"+substring(String(ob.year),3,2));
loc.text = myArray[2];
desc.text = myArray[3];
var oldt = getTimer();
var ok = true;
var l = myArray.length;
var i = 4;
_root.onEnterFrame = function(){
t = getTimer();
if(t-oldt>15000){
header.htmlText = myArray[i];
date.text = myArray[i+1];
loc.text = myArray[i+2];
desc.text = myArray[i+3];
if(i<(l-4)){
i += 4;
}else{
i = 0;
}
oldt = getTimer();
}
};
};
The swf only displays the "/" marks in the text field, but not the "ob.month" etc.
-
var x:Number = 1; x /= 0;
What is "myArray" for?
I don't understand what you are doing with this code:
Code:
temp.onLoad = function(){
myArray = new Array();
for(var a in this){
if(a != "onLoad"){
myArray.push(this[a]);
}
}
One thing, however, is that you should put the line "myArray=new Array();" outside of the temp.onLoad().
-
I was just using 'myArray' to call up the different arrays in the php script. For example, myArray[0] was calling '&desc=' and myArray[1] was calling '&loc='
That part seems to work.
Since that last post however, I found a solution to the unix timestamp in PHP, which I can't believe I didn't come across sooner. I just put:
Code:
print("&date$i=");
print(date("F dS Y h:i A", "$date"));
instead of:
Code:
print("&date$i=$date");
I am really so sorry I took up so much of your time with this, but thank you very much for all of your help! I appreciate it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|