twitter streaming api 2

Use the twitter stream API on PHP to store tweets on a database an retrieve them with jQuery
Original code twitter watch
Changes:
reverse chronological order (prepend instead of append tweets)
no tweet remove when we have more than nine
display avatar,name and difference on date like the ones twitter display
only add when we click de button that counts the new tweets
Example:

HTML: (add date.format.js)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gl" lang="gl">
<head>
  <title>Twitter alert viewer>/title>
  <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
  <link href="style.css" rel="stylesheet" type="text/css" />
  <script src="logic.js" type="text/javascript"></script>
  <script src="date.format.js" type="text/javascript"></script>
</head>
<body>
  <div id="tweets">
    <h1>Twitter alert viewer</h1>
    <a href="#" id="pause">Pause</a>
    <a href="#" id="run">Run</a>
    <div id="feed-container">
    </div>
  </div>
</body>
</html>

table.sql
CREATE TABLE IF NOT EXISTS `tweets` (
  `id` bigint(20) unsigned NOT NULL,
  `text` varchar(150) NOT NULL,
  `name` varchar(100) NOT NULL,
  `screen_name` varchar(255) NOT NULL,
  `followers_count` varchar(50) NOT NULL,
  `created_at` datetime NOT NULL,
  `image` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

watch.php
<?php
$db = mysql_connect('localhost', 'twitter_alerts', 'somepasword');
mysql_select_db('twitter_alerts', $db);
$opts = array(
 'http'=>array(
  'method'=> "POST",
  'content'=> 'track='.WORDS_TO_TRACK
 )
);
$context = stream_context_create($opts);
$instream = fopen('https://'.TWITTER_USERNAME.':'.TWITTER_PASSWORD.'@stream.twitter.com/1/statuses/filter.json','r',false,$context);
while(! feof($instream)) {
 if(! ($line = stream_get_line($instream, 20000, "\n"))) {
  continue;
 }else{
  $tweet = json_decode($line);
  if(($tweet->{'text'})||($tweet->{'text'}!="")){
   $id = $tweet->{'id_str'};
   $text = mysql_real_escape_string($tweet->{'text'});
   $date=new DateTime($tweet->{'created_at'}, new DateTimeZone('UTC')); 
   $time=$date->format("Y-m-d H:i:s");
   $name = mysql_real_escape_string($tweet->{'user'}->{'name'});
   $screen_name = mysql_real_escape_string($tweet->{'user'}->{'screen_name'});
   $followers_count = mysql_real_escape_string($tweet->{'user'}->{'followers_count'});
   $image_url=mysql_real_escape_string($tweet->{'user'}->{'profile_image_url'});
   $ok = mysql_query("INSERT INTO tweets (id ,text ,name,screen_name ,followers_count, created_at,image) VALUES ('$id', '$text', '$name' , '$screen_name', '$followers_count', '$time', '$image_url')");
   if (!$ok) {echo "Mysql Error: ".mysql_error();}
    flush();
  }
 }
}
?>

server.php
<?php
 $db = mysql_connect('localhost', 'twitter_alerts', 'somepasword');
 mysql_select_db('twitter_alerts');
 $start = mysql_real_escape_string($_GET['start']);
 if(! $start){
  $query = "SELECT * FROM tweets ORDER BY id ASC";
 }else{
  $query = "SELECT * FROM tweets WHERE id>".$start." ORDER BY id ASC";
 }
 $result = mysql_query($query);
 $data = array();
 while ($row = mysql_fetch_assoc($result)){
  array_push($data, $row);
 }
 $json = json_encode($data);
 print $json;
?>

logic.js (change to add new tweets count button and diff date like twitter)
var cTime;
var tTime;
var nTotalDiff;
var oDiff;
var Diff;
var last = '';
var init=false;
var timeOut;
var newtweets=0;
var tweets=new Array();
var newtweet=$('<div class="newtweet">');
$(newtweet).html('<div class="count">');
$(newtweet).on("click",function(){
 newtweets=0;
 $(this).remove();
 $("#feed-container").prepend(tweets.reverse());
 tweets=new Array();
 $(".created_at").diffdate();
});
function poll(){
  $.getJSON("server.php?start="+last,
   function(data){
    if(data.length>0&&init==true&&newtweets==0){
     $("#feed-container").prepend(newtweet);
     $(newtweet).on("click",function(){
      newtweets=0;
      $(this).remove();
      $("#feed-container").prepend(tweets.reverse());
      tweets=new Array();
      $(".created_at").diffdate();
     });
    }
     $.each(data, function(count,item){
       rgb = Math.floor(16*(Math.log(item.followers_count+1)+1));     
       var tweet=$('<div class="tweet" id="'+item.id+'">');
       $(tweet).html(
        '<a href="http://twitter.com/'+item.screen_name+'"><img class="avatar" src="'+item.image+'"></a>'+
        '<div class="namec"><a class="name" href="http://twitter.com/'+item.screen_name+'">'+item.name+'</a>'+
        '<a class="screen_name" href="http://twitter.com/'+item.screen_name+'">@'+item.screen_name+'</a>'+
        '<a href="https://twitter.com/'+item.screen_name+'/status/'+item.id+'" class="created_at">'+item.created_at+'</a></div>'
        +'<span class="text">'+item.text+'</span>'+
        '<div><a class="expand" href="https://twitter.com/'+item.screen_name+'/status/'+item.id+'">Expand</a></div>'
       );
       if(init==true){
        tweets.push(tweet);
        newtweets++;
        if(newtweets==1){
         $(newtweet).find(".count").html(newtweets+" new Tweet");
        }
        else{
         $(newtweet).find(".count").html(newtweets+" new Tweets");
        }
       }
       else{
        $('#feed-container').prepend(tweet);
       }
       last = item.id;
     });
     init=true;
     $(".created_at").diffdate();
   }
  );
  timeOut = setTimeout('poll()', 30000);
}
function jsdate(ele){
 var e=ele.split(/[- :]/);
 var d = new Date(Date.UTC(e[0], e[1]-1, e[2], e[3], e[4], e[5]));
 return d;
}
(function($){
  $.fn.extend({
   diffdate: function(){
   return this.each(function(){
    cTime=new Date();
    var ele=this;
    if(!$(ele).is(".difference")){
     this.createdat=$(ele).html();
     $(ele).addClass("difference");
    }
    tTime=jsdate(ele.createdat);
    nTotalDiff = cTime.getTime() - tTime.getTime();
    oDiff = new Object();
    oDiff.days = Math.floor(nTotalDiff/1000/60/60/24);
    nTotalDiff -= oDiff.days*1000*60*60*24;
    oDiff.hours = Math.floor(nTotalDiff/1000/60/60);
    nTotalDiff -= oDiff.hours*1000*60*60;
    oDiff.minutes = Math.floor(nTotalDiff/1000/60);
    nTotalDiff -= oDiff.minutes*1000*60;
    oDiff.seconds = Math.floor(nTotalDiff/1000);
    if(cTime.getTime()<=tTime.getTime()){
    Diff="now";
    }
    else if(oDiff.days==1){
    Diff=tTime.format("mmm d");
    }
    else if(oDiff.days>1){
    Diff=tTime.format("mmm d");
    }
    else if(oDiff.hours==1){
    Diff=oDiff.hours+" hr ";   
    }
    else if(oDiff.hours>1){
    Diff=oDiff.hours+" hrs ";   
    }
    else if(oDiff.minutes==1){
    Diff=oDiff.minutes+" min ";
    }
    else if(oDiff.minutes>1){
    Diff=oDiff.minutes+" mins ";
    }
    else if(oDiff.seconds==1){
    Diff=oDiff.seconds+" sec ";   
    }
    else if(oDiff.seconds>1){
    Diff=oDiff.seconds+" secs ";   
    }
    else{
    Diff="now";
    }
    $(ele).html(Diff).attr({"title":tTime.format("h:MM:ss tt - mmm d, yyyy")});
    });
   }
  });
 })(jQuery);
$(document).ready(function(){
 poll();
 $("#pause").click(function(e){
  e.preventDefault();
  clearTimeout(timeOut);
 });
 $("#run").click(function(e){
  e.preventDefault();
  poll();
 });
});

date.formatl.js (format date to look like the ones on tooltip)
/*
 * Date Format 1.2.3
 * (c) 2007-2009 Steven Levithan 
 * MIT license
 *
 * Includes enhancements by Scott Trenda 
 * and Kris Kowal 
 *
 * Accepts a date, a mask, or a date and a mask.
 * Returns a formatted version of the given date.
 * The date defaults to the current date/time.
 * The mask defaults to dateFormat.masks.default.
 */

var dateFormat = function () {
 var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
  timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
  timezoneClip = /[^-+\dA-Z]/g,
  pad = function (val, len) {
   val = String(val);
   len = len || 2;
   while (val.length < len) val = "0" + val;
   return val;
  };

 // Regexes and supporting functions are cached through closure
 return function (date, mask, utc) {
  var dF = dateFormat;

  // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
  if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
   mask = date;
   date = undefined;
  }

  // Passing date through Date applies Date.parse, if necessary
  date = date ? new Date(date) : new Date;
  if (isNaN(date)) throw SyntaxError("invalid date");

  mask = String(dF.masks[mask] || mask || dF.masks["default"]);

  // Allow setting the utc argument via the mask
  if (mask.slice(0, 4) == "UTC:") {
   mask = mask.slice(4);
   utc = true;
  }

  var _ = utc ? "getUTC" : "get",
   d = date[_ + "Date"](),
   D = date[_ + "Day"](),
   m = date[_ + "Month"](),
   y = date[_ + "FullYear"](),
   H = date[_ + "Hours"](),
   M = date[_ + "Minutes"](),
   s = date[_ + "Seconds"](),
   L = date[_ + "Milliseconds"](),
   o = utc ? 0 : date.getTimezoneOffset(),
   flags = {
    d:    d,
    dd:   pad(d),
    ddd:  dF.i18n.dayNames[D],
    dddd: dF.i18n.dayNames[D + 7],
    m:    m + 1,
    mm:   pad(m + 1),
    mmm:  dF.i18n.monthNames[m],
    mmmm: dF.i18n.monthNames[m + 12],
    yy:   String(y).slice(2),
    yyyy: y,
    h:    H % 12 || 12,
    hh:   pad(H % 12 || 12),
    H:    H,
    HH:   pad(H),
    M:    M,
    MM:   pad(M),
    s:    s,
    ss:   pad(s),
    l:    pad(L, 3),
    L:    pad(L > 99 ? Math.round(L / 10) : L),
    t:    H < 12 ? "a"  : "p",
    tt:   H < 12 ? "am" : "pm",
    T:    H < 12 ? "A"  : "P",
    TT:   H < 12 ? "AM" : "PM",
    Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
    o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
    S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
   };

  return mask.replace(token, function ($0) {
   return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
  });
 };
}();

// Some common format strings
dateFormat.masks = {
 "default":      "ddd mmm dd yyyy HH:MM:ss",
 shortDate:      "m/d/yy",
 mediumDate:     "mmm d, yyyy",
 longDate:       "mmmm d, yyyy",
 fullDate:       "dddd, mmmm d, yyyy",
 shortTime:      "h:MM TT",
 mediumTime:     "h:MM:ss TT",
 longTime:       "h:MM:ss TT Z",
 isoDate:        "yyyy-mm-dd",
 isoTime:        "HH:MM:ss",
 isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
 isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
 dayNames: [
  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
 ],
 monthNames: [
  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
 ]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
 return dateFormat(this, mask, utc);
};
style.css (change to look more like twitter)
body{
 font-family: georgia, serif;
 background:#777777;
}
#tweets{
 margin:0 auto;
 width:400px;
}
.container-top{
 border-bottom: 1px solid #DDDDDD;
    font-size: 18px;
    font-weight: bolder;
    height: 30px;
    padding-left: 10px;
    padding-top: 10px;
    position: absolute;
    top: 0;
    width: 100%;
}
.newtweet{
 background: none repeat scroll 0 0 #EEEEEE;
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.1) inset;
    cursor: pointer;
    height: 30px;
    padding-top: 10px;
    text-align: center;
 margin-bottom:1px;
}
.newtweet:hover{
 background: none repeat scroll 0 0 #DDDDDD;
}
.newtweet .tweet{
 display:none;
}
#feed-container{
 background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #AAAAAA;
    border-radius: 7px 7px 7px 7px;
    margin-top: 15px;
    overflow: hidden;
    padding-top: 40px;
    position: relative;
    width: 100%;
}
.tweet{
 border-bottom: 1px solid #dddddd;
    padding: 10px;
    position: relative;
    word-wrap: break-word;
}
.tweet:hover{
 background:#eeeeee;
}
.namec{
 left: 60px;
    position: absolute;
    top: 5px;
    width: 330px;
}
.created_at{
 color: #AAAAAA;
    position: absolute;
    right: 0;
 text-decoration:none;
}
.created_at:hover{
 color: #000000;
 text-decoration:underline;
}
.text{
 position:relative;
 margin-bottom:10px;
 padding-left:5px;
 top:-7px;
}
.avatar{
 border-radius: 5px 5px 5px 5px;
 height: 48px;
 width: 48px;
}
.name{
 color:#000000;
 font-weight:bolder;
 text-decoration:none;
}
.name:hover{
 text-decoration:underline;
}
.screen_name{
 color: #AAAAAA;
    margin-left: 5px;
    text-decoration: none;
}
.expand{
 color: #AAAAAA;
    left: 0;
    position: relative;
    text-decoration: none;
}
.expand:hover{
 color:#000000;
 text-decoration:underline;
}
.tweet:hover .expand{
 color:#000000;
}
Ver más

twitter streaming api

Use the twitter stream API on PHP to store tweets on a database an retrieve them with jQuery
Original code twitter watch
Changes:
reverse chronological order (prepend instead of append tweets)
no tweet remove when we have more than nine
display avatar,name and difference on date and now
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gl" lang="gl">
<head>
  <title>Twitter alert viewer>/title>
  <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
  <link href="style.css" rel="stylesheet" type="text/css" />
  <script src="logic.js" type="text/javascript"></script>
</head>
<body>
  <div id="tweets">
    <h1>Twitter alert viewer</h1>
    <a href="#" id="pause">Pause</a>
    <a href="#" id="run">Run</a>
    <div id="feed-container">
    </div>
  </div>
</body>
</html>

table.sql
CREATE TABLE IF NOT EXISTS `tweets` (
  `id` bigint(20) unsigned NOT NULL,
  `text` varchar(150) NOT NULL,
  `name` varchar(100) NOT NULL,
  `screen_name` varchar(255) NOT NULL,
  `followers_count` varchar(50) NOT NULL,
  `created_at` datetime NOT NULL,
  `image` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

watch.php
<?php
$db = mysql_connect('localhost', 'twitter_alerts', 'somepasword');
mysql_select_db('twitter_alerts', $db);
$opts = array(
 'http'=>array(
  'method'=> "POST",
  'content'=> 'track='.WORDS_TO_TRACK
 )
);
$context = stream_context_create($opts);
$instream = fopen('https://'.TWITTER_USERNAME.':'.TWITTER_PASSWORD.'@stream.twitter.com/1/statuses/filter.json','r',false,$context);
while(! feof($instream)) {
 if(! ($line = stream_get_line($instream, 20000, "\n"))) {
  continue;
 }else{
  $tweet = json_decode($line);
  if(($tweet->{'text'})||($tweet->{'text'}!="")){
   $id = $tweet->{'id_str'};
   $text = mysql_real_escape_string($tweet->{'text'});
   $date=new DateTime($tweet->{'created_at'}, new DateTimeZone('UTC')); 
   $time=$date->format("Y-m-d H:i:s");
   $name = mysql_real_escape_string($tweet->{'user'}->{'name'});
   $screen_name = mysql_real_escape_string($tweet->{'user'}->{'screen_name'});
   $followers_count = mysql_real_escape_string($tweet->{'user'}->{'followers_count'});
   $image_url=mysql_real_escape_string($tweet->{'user'}->{'profile_image_url'});
   $ok = mysql_query("INSERT INTO tweets (id ,text ,name,screen_name ,followers_count, created_at,image) VALUES ('$id', '$text', '$name' , '$screen_name', '$followers_count', '$time', '$image_url')");
   if (!$ok) {echo "Mysql Error: ".mysql_error();}
    flush();
  }
 }
}
?>

server.php
<?php
 $db = mysql_connect('localhost', 'twitter_alerts', 'somepasword');
 mysql_select_db('twitter_alerts');
 $start = mysql_real_escape_string($_GET['start']);
 if(! $start){
  $query = "SELECT * FROM tweets ORDER BY id ASC";
 }else{
  $query = "SELECT * FROM tweets WHERE id>".$start." ORDER BY id ASC";
 }
 $result = mysql_query($query);
 $data = array();
 while ($row = mysql_fetch_assoc($result)){
  array_push($data, $row);
 }
 $json = json_encode($data);
 print $json;
?>

logic.js
var cTime;
var tTime;
var nTotalDiff;
var oDiff;
var dateDiff;
var last = '';
var init=false;
var timeOut;
function poll(){
  $.getJSON("server.php?start="+last,
   function(data){
     $.each(data, function(count,item){
       rgb = Math.floor(16*(Math.log(item.followers_count+1)+1));
       importanceColor='rgb('+rgb+',0,0)';      
       var tweet=$('<div class="tweet" id="'+item.id+'">');
       $(tweet).html(
        '<a href="http://twitter.com/'+item.screen_name+'"><img class="avatar" src="'+item.image+'"></a>'+
        '<div class="namec"><a class="name" href="http://twitter.com/'+item.screen_name+'">'+item.name+'</a>'+
        '<a class="screen_name" href="http://twitter.com/'+item.screen_name+'" style="color:'+importanceColor+'">@'+item.screen_name+'</a>'+
        '<a class="expand" href="https://twitter.com/'+item.screen_name+'/status/'+item.id+'">Expand</a></div>'
        +'<span class="text">'+item.text+'</span>'+
        '<div class="created_at">'+item.created_at+'</div>'
       );
       $('#feed-container').prepend(tweet);
       if(init==true){
        var h=$(tweet).height();
        $(tweet).css({"height":"0px"}).animate({"height":h},2000);
       }
       last = item.id;
     });
     init=true;
     $(".created_at").diffdate();
   }
  );
  timeOut = setTimeout('poll()', 30000);
}
function jsdate(ele){
 var e=ele.split(/[- :]/);
 var d = new Date(Date.UTC(e[0], e[1]-1, e[2], e[3], e[4], e[5]));
 return d;
}
(function($){
  $.fn.extend({
   diffdate: function(){
   return this.each(function(){
     dateDiff="";
     cTime=new Date();
    var ele=this;
    if(!$(ele).is(".difference")){
     this.createdat=$(ele).html();
     $(ele).addClass("difference");
    }
    tTime=jsdate(ele.createdat);
    nTotalDiff = cTime.getTime() - tTime.getTime();
    oDiff = new Object();
    oDiff.days = Math.floor(nTotalDiff/1000/60/60/24);
    nTotalDiff -= oDiff.days*1000*60*60*24;
    oDiff.hours = Math.floor(nTotalDiff/1000/60/60);
    nTotalDiff -= oDiff.hours*1000*60*60;
    oDiff.minutes = Math.floor(nTotalDiff/1000/60);
    nTotalDiff -= oDiff.minutes*1000*60;
    oDiff.seconds = Math.floor(nTotalDiff/1000);
    
    if(oDiff.days==1){
    dateDiff+=oDiff.days+" day ";
    }
    else if(oDiff.days>1){
    dateDiff+=oDiff.days+" days ";
    }
    if(oDiff.hours==1){
    dateDiff+=oDiff.hours+" hr ";   
    }
    else if(oDiff.hours>1){
    dateDiff+=oDiff.hours+" hrs ";   
    }
    if(oDiff.minutes==1){
    dateDiff+=oDiff.minutes+" min ";
    }
    else if(oDiff.minutes>1){
    dateDiff+=oDiff.minutes+" mins ";
    }
    if(oDiff.seconds==1){
    dateDiff+=oDiff.seconds+" sec ";   
    }
    else if(oDiff.seconds>1){
    dateDiff+=oDiff.seconds+" secs ";   
    }
    $(ele).html(dateDiff);
    });
   }
  });
 })(jQuery);
$(document).ready(function(){
 poll();
 $("#pause").click(function(e){
  e.preventDefault();
  clearTimeout(timeOut);
 });
 $("#run").click(function(e){
  e.preventDefault();
  poll();
 });
});
style.css
body{
 font-family: georgia, serif;
}
#tweets{
 margin:0 auto;
 width:400px;
}
#feed-container{
 width:100%;
 height:500px;
 overflow:auto; 
}
.tweet{
 border:1px solid silver;
 margin:5px;
 padding:5px;
 background-color:#fff;
 word-wrap:break-word;
 position:relative;
}
.namec{
 left: 60px;
 position: absolute;
 top: 5px;
 width: 300px;
}
.created_at{
 position:relative;
 top:5px;
 padding-bottom:3px;
 color: #AAAAAA;
}
.text{
 position:relative;
 margin-bottom:10px;
 padding-left:5px;
 top:-7px;
}
.avatar{
 border-radius: 5px 5px 5px 5px;
 height: 48px;
 width: 48px;
}
.name{
 color:#000000;
 font-weight:bolder;
 text-decoration:none;
}
.name:hover{
 text-decoration:underline;
}
.screen_name{
 margin-left:5px;
}
.expand{
 position:absolute;
 right:0px;
 text-decoration:none;
 color: #AAAAAA;
}
.expand:hover{
 color:#000000;
 text-decoration:underline;
}

Example:
Ver más

Store utc date time on mysql

Example of the differences between timezones and the use of a function to convert "date()" and "DateTime()" from utc to another timezone or change "DateTime()" timezone with setTimeZone()

Set timezone:
<?php
$date=new DateTime("NOW"); 
echo $date->format('M j Y g:i:s a e');    //Mar 6 2013 11:14:58 pm Europe/Berlin

$date=new DateTime("NOW", new DateTimeZone('UTC')); 
echo $date->format('M j Y g:i:s a e');    //Mar 6 2013 10:14:58 pm UTC

echo date('M j Y g:i:s a e');            //Mar 6 2013 11:14:58 pm Europe/Berlin

date_default_timezone_set('UTC');
echo date('M j Y g:i:s a e');            //Mar 6 2013 10:14:58 pm UTC
?>

Database Table

Store UTC datetime:
<?php
date_default_timezone_set('UTC');
$date=new DateTime("NOW", new DateTimeZone('UTC')); 
//mysql with date()
$link= mysql_connect('SERVER','USER','PASS');
if(!$link){
   die('Not connected : ' . mysql_error());
}
$db=mysql_select_db('DATABASE', $link);
if(!$db){
   die('Can\'t use database : '.mysql_error());
}
$query=mysql_query("INSERT INTO TABLE (utc_date_time) VALUES ('".date('Y-m-d H:i:s')."')");
if (!$query){
   echo 'Mysql Error: '.mysql_error();
}
mysql_close($link);

//mysqli object oriented with DateTime()
$mysqli= new mysqli('SERVER','USER','PASS','DATABASE');
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
$mysqli->query("INSERT INTO TABLE (utc_date_time) VALUES ('".$date->format('Y-m-d H:i:s')."')");
$mysqli->close();

//mysqli procedural style with date()
$link= mysqli_connect('SERVER','USER','PASS','DATABASE');
if(mysqli_connect_errno()){
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
mysqli_query($link,"INSERT INTO TABLE (utc_date_time) VALUES ('".date('Y-m-d H:i:s')."')");
mysqli_close($link);
?>


Change timezone:
<?php
//date or DateTime,DateTimeZone,format (default mysql datetime format)
function utc_to_local($datetime, $time_zone,$format_string="Y-m-d H:i:s") 
{ 
 if($datetime instanceof DateTime){  //if $datetime is instance of DateTime don't set DateTimeZone
  $date = $datetime;
 }else{  //default DateTimeZone set to UTC
  $date = new DateTime($datetime, new DateTimeZone('UTC')); 
 }
 $date->setTimeZone(new DateTimeZone($time_zone));  //change timezone 
 return $date->format($format_string); 
} 

$date=new DateTime("NOW"); 
echo $date->format('M j Y g:i:s a e');   //Mar 7 2013 12:07:02 am Europe/Berlin
echo utc_to_local($date,'America/Mexico_City','M j Y g:i:s a e'); //Mar 6 2013 5:07:02 pm America/Mexico_City

$date=new DateTime("NOW", new DateTimeZone('UTC'));   
echo $date->format('M j Y g:i:s a e');                   //Mar 6 2013 11:07:02 pm UTC
echo utc_to_local($date,'Asia/Tokyo','M j Y g:i:s a e'); //Mar 7 2013 8:07:02 am Asia/Tokyo

$date=date("Y-m-d H:i:s");
echo $date;                  //2013-03-07 00:07:02

date_default_timezone_set('UTC');
$date=date("Y-m-d H:i:s");   
echo $date;                 //2013-03-06 23:07:02
echo utc_to_local($date,'Europe/London','M j Y g:i:s a e'); //Mar 6 2013 11:07:02 pm Europe/London

$date=date('M j Y g:i:s a e');
echo $date;                 //Mar 6 2013 11:07:02 pm UTC
echo utc_to_local($date,'Antarctica/South_Pole'); //2013-03-07 12:07:02

$date="Mar 6 2013 7:43:12 pm UTC";
echo $date;                //Mar 6 2013 7:43:12 pm UTC
echo utc_to_local($date,'Africa/Lagos'); //2013-03-06 20:43:12
?>
Ver más

spacegallery 2

Changes:
instead of animating the gallery with "spacegallery: 20", animates each image to the next position stored in an array.
the user can change the perspective on the X axis and the autoplay direction.
the border property is only needed on css.
Options:
perspective:140,     //Perspective height
minScale:0.2,        //Minimum scale
duration:800,        //Animation duration
loadingClass:null,   //CSS class of the element while loading
imageWidth:200,      //Max width of the images
scroll:false,        //Control gallery on mousewheel
buttons:false,       //Next,Prev,Play buttons
direct:false,        //Control gallery on image click
before: function(){return false},//Callback before
after: function(){return false},//Callback after
play:false,          //Autoplay
pause:5000,          //Autoplay pause
alpha:false          //opacity  of the images
direction:"forward", //Autoplay direction 
perspectivex:0       //X axis perspective

Example:
image1 image2 image3 image4 image5 image6

$("#mygallery").spacegallery({
        imageWidth:240,
        scroll:true,
        buttons:true,
        direct:true,
        play:true,
        alpha:true,
        duration:1000,
        direction:"backward",
        perspectivex:-140,
        loadingClass:"loading"
});

image1 image2 image3 image4 image5 image6

$("#mygallery").spacegallery({
        imageWidth:300,
        scroll:false,
        buttons:false,
        direct:true,
        play:false,
        alpha:false,
        duration:2000,
        perspectivex:0,
        loadingClass:"loading"
});


/**
 *
 * Spacegallery
 * Author: Stefan Petre www.eyecon.ro
 * 
 */ 
$.fn.spacegallery=function(method){
 var defaults={
  perspective:140,
  minScale:0.2,
  duration:800,
  loadingClass:null,
  imageWidth:200,
  scroll:false,
  buttons:false,
  direct:false,
  before: function(){return false},
  after: function(){return false},
  play:false,
  pause:5000,
  alpha:false,
  direction:"forward",
  perspectivex:0
 };
 var methods={
  init:function(opt){
   opt.imageWidth=opt.imageWidth||$(this).width();
   opt=$.extend({},defaults,opt||{});
   return this.each(function(){
    var el=this;
    el.spacegalleryCfg=opt;
    $(el).addClass(el.spacegalleryCfg.loadingClass);
    el.spacegalleryCfg.imgs=$(el).find("img");
    el.spacegalleryCfg.images=el.spacegalleryCfg.imgs.length;
    el.spacegalleryCfg.loaded=0;
    el.spacegalleryCfg.asin=Math.asin(1);
    el.spacegalleryCfg.asins={};
    el.spacegalleryCfg.tops={};
    el.spacegalleryCfg.lefts={};
    el.spacegalleryCfg.increment=parseInt(el.spacegalleryCfg.perspective/el.spacegalleryCfg.images);
    el.spacegalleryCfg.incrementx=el.spacegalleryCfg.perspectivex/el.spacegalleryCfg.images;
    var left=el.spacegalleryCfg.perspectivex;
    var top=0;
    el.spacegalleryCfg.dif=(el.spacegalleryCfg.imageWidth-el.spacegalleryCfg.imageWidth*el.spacegalleryCfg.minScale);
    el.spacegalleryCfg.imgs.each(function(index){
     var imgEl=new Image();
     imgEl.src=this.src;
     this.spacegallery={};
     el.spacegalleryCfg.asins[index]=1-Math.asin((index+1)/el.spacegalleryCfg.images)/el.spacegalleryCfg.asin;
     top+=parseInt(el.spacegalleryCfg.increment-el.spacegalleryCfg.increment*el.spacegalleryCfg.asins[index]);
     this.spacegallery.width=parseInt(el.spacegalleryCfg.imageWidth-el.spacegalleryCfg.dif*el.spacegalleryCfg.asins[index]);
     left-=el.spacegalleryCfg.incrementx;
     if(el.spacegalleryCfg.perspectivex>0){
      el.spacegalleryCfg.lefts[index]=parseInt(((el.spacegalleryCfg.imageWidth-this.spacegallery.width)))+left;
     }
     else if(el.spacegalleryCfg.perspectivex<0){
      el.spacegalleryCfg.lefts[index]=parseInt(((el.spacegalleryCfg.imageWidth-el.spacegalleryCfg.imageWidth))/2)+left;
     }
     else{
      el.spacegalleryCfg.lefts[index]=parseInt(((el.spacegalleryCfg.imageWidth-this.spacegallery.width))/2);
     }
     el.spacegalleryCfg.tops[index]=top;
     $(this).width(el.spacegalleryCfg.imageWidth).css({"position":"absolute"});   
     if (imgEl.complete) {
      el.spacegalleryCfg.loaded++;
      if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images){
        $(el).spacegallery("positionImages");
      }
     }
     else {
      imgEl.onload=function(){
       el.spacegalleryCfg.loaded++;
       if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images) {
        $(el).spacegallery("positionImages");
       }
      }
     }
    });
    el.spacegalleryCfg.asins[el.spacegalleryCfg.images]=el.spacegalleryCfg.asins[0]*.7;
    el.spacegalleryCfg.tops[el.spacegalleryCfg.images]=parseInt(el.spacegalleryCfg.tops[0]*.7);
    el.spacegalleryCfg.asins[el.spacegalleryCfg.images+1]=el.spacegalleryCfg.asins[el.spacegalleryCfg.images-1]*1.3;
    el.spacegalleryCfg.tops[el.spacegalleryCfg.images+1]=parseInt(el.spacegalleryCfg.tops[el.spacegalleryCfg.images-1]*1.3);
    if(el.spacegalleryCfg.scroll==true){
     $(el).bind((/Firefox/i.test(navigator.userAgent))?"DOMMouseScroll":"mousewheel",function(e){
      var evt=window.event||e;
      evt=evt.originalEvent?evt.originalEvent:evt;
      var delta=evt.detail?evt.detail*(-40):evt.wheelDelta;
      if(el.spacegalleryCfg.interval){
       el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);
      }
      if(delta > 0) {
       $(el).spacegallery("prev");
      }
      else{
       $(el).spacegallery("next");
      }
     });
    }
    if(el.spacegalleryCfg.buttons==true){
     $(el).append("<div class='prev'></div>").append("<div class='next'></div>").append("<div class='play'></div>");
     $(el).find(".prev").bind("click",function(){
      if(el.spacegalleryCfg.interval){
       el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);
      }
      $(el).spacegallery("prev");
     });
     $(el).find(".next").bind("click",function(){
      if(el.spacegalleryCfg.interval){
       el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);
      }
      $(el).spacegallery("next");
     });
     $(el).find(".play").bind("click",function(){
      $(el).spacegallery("play");
     });
     if(el.spacegalleryCfg.play==false){
      $(el).find(".play").addClass("pause");
     }
    }
    if(el.spacegalleryCfg.loaded==el.spacegalleryCfg.images){
     $(el).spacegallery("positionImages");
     if(el.spacegalleryCfg.play==true){
      el.spacegalleryCfg.interval=setInterval(function(){
       if(el.spacegalleryCfg.direction=="backward"){
        $(el).spacegallery("prev");
       }
       else{
        $(el).spacegallery("next");       
       }
      }, el.spacegalleryCfg.pause);
     }
    }
   });
  },
  positionImages:function(){
   return this.each(function(){
    var el=this;
    el.spacegalleryCfg.animated=false;
    $(el).find("img").removeAttr('height').each(function(index){
     this.spacegallery.width=parseInt(el.spacegalleryCfg.imageWidth-el.spacegalleryCfg.dif*el.spacegalleryCfg.asins[index]);
     $(this).css({
      "opacity":function(){
       if(el.spacegalleryCfg.alpha==true){
        return 1 - el.spacegalleryCfg.asins[index];
       }
       else{
        return 1;
       }
      },
      "width":this.spacegallery.width,
      "top":el.spacegalleryCfg.tops[index],
      "margin-left":el.spacegalleryCfg.lefts[index]
     });
     if(el.spacegalleryCfg.direct==true){
      $(this).off('click').click(function(){
       clearInterval(el.spacegalleryCfg.interval);
       $(el).spacegallery("to",index);
      });
     }
     if(index!=0){
      this.spacegallery.prev=el.spacegalleryCfg.asins[index-1];
      this.spacegallery.prevTop=el.spacegalleryCfg.tops[index-1];
      this.spacegallery.prevleft=el.spacegalleryCfg.lefts[index-1];
     }
     else{
      this.spacegallery.prev=el.spacegalleryCfg.asins[el.spacegalleryCfg.images];
      this.spacegallery.prevTop=el.spacegalleryCfg.tops[el.spacegalleryCfg.images];
      this.spacegallery.prevleft=el.spacegalleryCfg.lefts[el.spacegalleryCfg.images-1];
     }
     if(index==el.spacegalleryCfg.images-1){
      this.spacegallery.next=el.spacegalleryCfg.asins[el.spacegalleryCfg.images+1];
      this.spacegallery.nextTop=el.spacegalleryCfg.tops[el.spacegalleryCfg.images+1];
      this.spacegallery.nextleft=el.spacegalleryCfg.lefts[0];
     }
     else{
      this.spacegallery.next=el.spacegalleryCfg.asins[index+1];
      this.spacegallery.nextTop=el.spacegalleryCfg.tops[index+1];
      this.spacegallery.nextleft=el.spacegalleryCfg.lefts[index+1];
     }
     this.spacegallery.origTop=el.spacegalleryCfg.tops[index];
     this.spacegallery.increment=el.spacegalleryCfg.asins[index]-this.spacegallery.next;
     this.spacegallery.nextwidth=el.spacegalleryCfg.imageWidth-el.spacegalleryCfg.dif*this.spacegallery.next;
     this.spacegallery.prevwidth=el.spacegalleryCfg.imageWidth-el.spacegalleryCfg.dif*this.spacegallery.prev;
    });
    $(el).removeClass(el.spacegalleryCfg.loadingClass);
   });
  },
  next:function(num){
   return this.each(function(){
    var el=this;
    if(el.spacegalleryCfg.animated==false){
     el.spacegalleryCfg.animated=true;
     el.spacegalleryCfg.before.apply();
     var c=0;
     $(el).find('img').each(function(nr){
      var newWidth=this.spacegallery.nextwidth;
      var newLeft=this.spacegallery.nextleft;
      var opacity=1;
      if(el.spacegalleryCfg.alpha==true){
       opacity=1-this.spacegallery.next;
      }
      if(nr==el.spacegalleryCfg.images-1){
       newWidth=this.spacegallery.width*this.spacegallery.nextTop/this.spacegallery.origTop;
       opacity=0;
       newLeft=-((newWidth-this.spacegallery.width)/2);
       newLeft=newLeft-el.spacegalleryCfg.incrementx*2;
      }
      $(this).stop(true).animate({
        "top":this.spacegallery.nextTop,
        "width":parseInt(newWidth),
        "margin-left":newLeft,
        "opacity":opacity
       },
       el.spacegalleryCfg.duration,
       function(){
        c++;
        if(c==el.spacegalleryCfg.images){
         var last=$(el).find('img:last');
         $(last).prependTo(el);
         if(el.spacegalleryCfg.alpha==true){
          $(last).animate({"opacity":1-el.spacegalleryCfg.asins[0]});
         }
         else{
          $(last).animate({"opacity":1});
         }
         $(el).spacegallery("positionImages");
         $(last).css({"opacity":0});
         el.spacegalleryCfg.after.apply();
         if(num>1){
          num--;
          $(el).spacegallery("next",num);
         }
         else if((el.spacegalleryCfg.play==true&&num==1)||(el.spacegalleryCfg.play==true&&!num&&!el.spacegalleryCfg.interval)){
          el.spacegalleryCfg.interval=setInterval(function(){
           if(el.spacegalleryCfg.direction=="backward"){
            $(el).spacegallery("prev");
           }
           else{
            $(el).spacegallery("next");       
           }
          },el.spacegalleryCfg.pause);
         }
        }
       }
      );
     });
    }
   });
  },
  prev:function(num){
   return this.each(function(){
    var el=this;
    if(el.spacegalleryCfg.animated==false){
     el.spacegalleryCfg.animated=true;
     el.spacegalleryCfg.before.apply();
     var c=0;
     $(el).find('img').each(function(nr){
      var newWidth =  this.spacegallery.prevwidth;
      var opacity=1;
      var newLeft=this.spacegallery.prevleft;
      if(el.spacegalleryCfg.alpha==true){
       opacity=1-this.spacegallery.prev;
      }
      if(nr==0){
       newWidth=this.spacegallery.width*this.spacegallery.prevTop/this.spacegallery.origTop;
       opacity=0;
       newLeft=((el.spacegalleryCfg.imageWidth-newWidth)/2);
       newLeft=newLeft+el.spacegalleryCfg.perspectivex*1.8;
      }
      $(this).stop(true).animate({
       "top":this.spacegallery.prevTop,
       "width":parseInt(newWidth),
       "margin-left":newLeft,
       "opacity":opacity
       },
       el.spacegalleryCfg.duration,
       function(){
        c++;
        if(c==el.spacegalleryCfg.images){
         $(el).find('img:first').appendTo(el).css({"opacity":0,"margin-left":0}).animate({"opacity":1});
         $(el).spacegallery("positionImages");
         el.spacegalleryCfg.after.apply();
         if(num>1){
          num--;
          $(el).spacegallery("prev",num);
         }
         else if((el.spacegalleryCfg.play==true&&num==1)||(el.spacegalleryCfg.play==true&&!num&&!el.spacegalleryCfg.interval)){
          el.spacegalleryCfg.interval=setInterval(function(){
           if(el.spacegalleryCfg.direction=="backward"){
            $(el).spacegallery("prev");
           }
           else{
            $(el).spacegallery("next");       
           }
          },el.spacegalleryCfg.pause);
         }
        }
       }
      );
     });
    }
   });
  },
  to:function(num){
   return this.each(function(){
    var el=this;
    if(num!=el.spacegalleryCfg.images-1){
     var dif=0;
     if(num<=parseInt((el.spacegalleryCfg.images-1)/2)){
      dif=el.spacegalleryCfg.images-(el.spacegalleryCfg.images-1-num);
      $(el).spacegallery("prev",dif);
     }
     else{
      dif=el.spacegalleryCfg.images-1-num;
      $(el).spacegallery("next",dif);
     }
    }
   });
  },
  play:function(){
   return this.each(function() {
    var el=this;
    if(el.spacegalleryCfg.play==false){
     el.spacegalleryCfg.play=true;
     $(el).find(".play").removeClass("pause");
     if(el.spacegalleryCfg.animated==false){
      el.spacegalleryCfg.interval=setInterval(function(){
       if(el.spacegalleryCfg.direction=="backward"){
        $(el).spacegallery("prev");
       }
       else{
        $(el).spacegallery("next");       
       }
      },el.spacegalleryCfg.pause);
     }
    }
    else{
     if(el.spacegalleryCfg.interval){
      el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);
      el.spacegalleryCfg.play=false;
      $(el).find(".play").addClass("pause");
     }
    }
   });
  }
 };
    if(methods[method]){
  return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
    }else if(typeof method==='object'||!method){
  return methods.init.apply(this,arguments);
    }else{
  $.error('Method '+method+' does not exist on jQuery.spacegallery');
    }
};
Ver más

spacegallery mod


The original code for spacegallery
Changes:

Include Javascript
<script type="text/javascript" src="spacegallery.js"></script>

Invocation code:
$('#myGallery').spacegallery();           //Or
$('#myGallery').spacegallery(options);
$('#myGallery').spacegallery("method");   //String method name

Options:
perspective:140,  //Perspective height
minScale:0.2,     //Minimum scale
duration:800,     //Animation duration
loadingClass:null,//CSS class of the element while loading
imageWidth:200,   //Max width of the images
scroll:false,     //Control gallery on mousewheel
buttons:false,    //Next,Prev,Play buttons
direct:false,     //Control gallery on image click
before: function(){return false},//Callback before
after: function(){return false},//Callback after
play:false,       //Autoplay
pause:5000,       //Autoplay pause
alpha:false       //opacity  of the images

Example:
<html>
<body>
  <div id="myGallery" class="loading">
      <img src="spacegallery/images/bw3.jpg" alt="" />
      <img src="spacegallery/images/lights3.jpg" alt="" />
      <img src="spacegallery/images/bw2.jpg" alt="" />
      <img src="spacegallery/images/lights2.jpg" alt="" />
      <img src="spacegallery/images/bw1.jpg" alt="" />
      <img src="spacegallery/images/lights1.jpg" alt="" />
 </div>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="spacegallery.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
    $("#myGallery").spacegallery({
      imageWidth:240,
      scroll:true,
      buttons:true,
      direct:true,
      play:true,
      alpha:true,
      duration:5000,
      loadingClass:"loading"
    });
});
</script>
</body>
</html>

spacegallery.js
/**
 *
 * Spacegallery
 * Author: Stefan Petre www.eyecon.ro
 * 
 */ 
$.fn.spacegallery=function(method){
 var defaults={
  perspective:140,
  minScale:0.2,
  duration:800,
  loadingClass:null,
  imageWidth:200,
  scroll:false,
  buttons:false,
  direct:false,
  before: function(){return false},
  after: function(){return false},
  play:false,
  pause:5000,
  alpha:false
 };
 var methods = {
  init : function( opt ) {
   opt.imageWidth=opt.imageWidth||$(this).width();
   opt = $.extend({}, defaults, opt||{});
   return this.each(function() {
    var el=this;
    el.spacegalleryCfg=opt;
    $(el).addClass(el.spacegalleryCfg.loadingClass);
    el.spacegalleryCfg.imgs=$(el).find("img");
    el.spacegalleryCfg.images=el.spacegalleryCfg.imgs.length;
    el.spacegalleryCfg.loaded=0;
    el.spacegalleryCfg.asin=Math.asin(1);
    el.spacegalleryCfg.asins={};
    el.spacegalleryCfg.tops={};
    el.spacegalleryCfg.increment = parseInt(el.spacegalleryCfg.perspective/el.spacegalleryCfg.images,10);
    var top = 0;
    el.spacegalleryCfg.imgs.each(function(index){
     var imgEl = new Image();
     imgEl.src = this.src;
     this.spacegalleryCfg={};
     el.spacegalleryCfg.asins[index]=1-Math.asin((index+1)/el.spacegalleryCfg.images)/el.spacegalleryCfg.asin;
     top+=parseInt(el.spacegalleryCfg.increment-el.spacegalleryCfg.increment*el.spacegalleryCfg.asins[index]);
     el.spacegalleryCfg.tops[index]=top;
     
     $(this).width(el.spacegalleryCfg.imageWidth).css({"position":"absolute"});
     
     this.spacegalleryCfg.origWidth=el.spacegalleryCfg.imageWidth;
   
     if (imgEl.complete) {
      el.spacegalleryCfg.loaded++;
      this.spacegalleryCfg.origHeight = imgEl.height
      if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images) {
        $(el).spacegallery("positionImages");
      }
     }
     else {
      imgEl.onload = function() {
       el.spacegalleryCfg.loaded++;
       this.spacegalleryCfg.origHeight = imgEl.height
       if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images) {
        $(el).spacegallery("positionImages");
       }
      }
     }
    });
    
    el.spacegalleryCfg.asins[el.spacegalleryCfg.images]=el.spacegalleryCfg.asins[0]*.7;
    el.spacegalleryCfg.tops[el.spacegalleryCfg.images]=parseInt(el.spacegalleryCfg.tops[0]*.7);
    el.spacegalleryCfg.asins[el.spacegalleryCfg.images+1]=el.spacegalleryCfg.asins[el.spacegalleryCfg.images-1]*1.3;
    el.spacegalleryCfg.tops[el.spacegalleryCfg.images+1]=parseInt(el.spacegalleryCfg.tops[el.spacegalleryCfg.images-1]*1.3);
    
    if(el.spacegalleryCfg.scroll==true){
     $(el).bind((/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel", function(e){
      var evt = window.event || e;   
      evt = evt.originalEvent ? evt.originalEvent : evt;              
      var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta;
      if(el.spacegalleryCfg.interval){
       el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);
      }
      if(delta > 0) {
       $(el).spacegallery("prev");
      }
      else{
       $(el).spacegallery("next");
      }   
     });
    }
    if(el.spacegalleryCfg.buttons==true){
     $(el).append("").append("").append("
");      $(el).find(".prev").bind("click",function(){       if(el.spacegalleryCfg.interval){        el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);       }       $(el).spacegallery("prev");      });      $(el).find(".next").bind("click",function(){       if(el.spacegalleryCfg.interval){        el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);       }       $(el).spacegallery("next");      });      $(el).find(".play").bind("click",function(){       $(el).spacegallery("play");      });      if(el.spacegalleryCfg.play==false){       $(el).find(".play").addClass("pause");      }     }     if (el.spacegalleryCfg.loaded == el.spacegalleryCfg.images) {      $(el).spacegallery("positionImages");      if(el.spacegalleryCfg.play==true){       el.spacegalleryCfg.interval=setInterval(function() {         $(el).spacegallery("next");        }, el.spacegalleryCfg.pause);      }     }    });   },   positionImages : function() {     return this.each(function() {     var el=this;     el.spacegalleryCfg.animated=false;     $(el).find("img").removeAttr('height').each(function(index){      this.spacegalleryCfg.width=parseInt(this.spacegalleryCfg.origWidth-(this.spacegalleryCfg.origWidth-this.spacegalleryCfg.origWidth*el.spacegalleryCfg.minScale)*el.spacegalleryCfg.asins[index]);      this.spacegalleryCfg.left=parseInt((el.spacegalleryCfg.imageWidth-this.spacegalleryCfg.width)/2);      $(this).css({       "opacity":function(){        if(el.spacegalleryCfg.alpha==true){         return 1 - el.spacegalleryCfg.asins[index];        }        else{         return 1;        }       },       "width":this.spacegalleryCfg.width+"px",       "margin-left":this.spacegalleryCfg.left+"px",       "top":el.spacegalleryCfg.tops[index]+"px"      });      if(el.spacegalleryCfg.direct==true){       $(this).off('click').click(function(){        clearInterval(el.spacegalleryCfg.interval);        $(el).spacegallery("to",index);       });      }      if(index!=0){       this.spacegalleryCfg.prev=el.spacegalleryCfg.asins[index-1];       this.spacegalleryCfg.prevTop=el.spacegalleryCfg.tops[index-1];      }      else{       this.spacegalleryCfg.prev=el.spacegalleryCfg.asins[el.spacegalleryCfg.images];       this.spacegalleryCfg.prevTop=el.spacegalleryCfg.tops[el.spacegalleryCfg.images];      }      if(index==el.spacegalleryCfg.images-1){       this.spacegalleryCfg.next=el.spacegalleryCfg.asins[el.spacegalleryCfg.images+1];       this.spacegalleryCfg.nextTop=el.spacegalleryCfg.tops[el.spacegalleryCfg.images+1];      }      else{       this.spacegalleryCfg.next=el.spacegalleryCfg.asins[index+1];       this.spacegalleryCfg.nextTop=el.spacegalleryCfg.tops[index+1];      }      this.spacegalleryCfg.origTop=el.spacegalleryCfg.tops[index];      this.spacegalleryCfg.zindex=1000-index;      this.spacegalleryCfg.increment=el.spacegalleryCfg.asins[index]-this.spacegalleryCfg.next;      this.spacegalleryCfg.current=el.spacegalleryCfg.asins[index];     });      $(el).removeClass(el.spacegalleryCfg.loadingClass);        });      },   next : function(num) {    return this.each(function() {     var el=this;     if(el.spacegalleryCfg.animated==false){      el.spacegalleryCfg.animated=true;      el.spacegalleryCfg.before.apply();      $(el).css('spacegallery', 0).animate({spacegallery:20},{       easing:'easeOut',       duration: el.spacegalleryCfg.duration,       complete: function() {        var last=$(el).find('img:last');        $(last).prependTo(el).css({"opacity":0});        if(el.spacegalleryCfg.alpha==true){         $(last).animate({"opacity":1-el.spacegalleryCfg.asins[0]});        }        else{         $(last).animate({"opacity":1});        }        $(el).spacegallery("positionImages");        el.spacegalleryCfg.after.apply();        if(num>1){         num--;         $(el).spacegallery("next",num);        }        else if((el.spacegalleryCfg.play==true&&num==1)||(el.spacegalleryCfg.play==true&&!num&&!el.spacegalleryCfg.interval)){         el.spacegalleryCfg.interval=setInterval(function() {           $(el).spacegallery("next");          }, el.spacegalleryCfg.pause);        }       },       step: function(now) {        $('img', this).each(function(nr){         var newWidth, top, next;         if (nr == el.spacegalleryCfg.images-1) {          top = this.spacegalleryCfg.origTop - ((this.spacegalleryCfg.origTop-this.spacegalleryCfg.nextTop )* now /20);          newWidth = this.spacegalleryCfg.width * top / this.spacegalleryCfg.origTop;          $(this).css({           top: parseInt(top) + 'px',           marginLeft: - parseInt((newWidth-el.spacegalleryCfg.imageWidth)/2, 10) + 'px',           width:parseInt(newWidth)+"px",           opacity:1-now/20          });         } else {          top = this.spacegalleryCfg.origTop -  ((this.spacegalleryCfg.origTop-this.spacegalleryCfg.nextTop )* now /20);          next = this.spacegalleryCfg.current - (this.spacegalleryCfg.current-this.spacegalleryCfg.next) * now /20;          newWidth =  this.spacegalleryCfg.origWidth - (this.spacegalleryCfg.origWidth - this.spacegalleryCfg.origWidth * el.spacegalleryCfg.minScale)*next;          $(this).css({           top: parseInt(top)+ 'px',           marginLeft: - parseInt((newWidth-el.spacegalleryCfg.imageWidth)/2, 10) + 'px',           width:parseInt(newWidth)+"px",           opacity:function(){              if(el.spacegalleryCfg.alpha==true){               return 1 -next;              }              else{               return 1;              }             }          });         }        });       }      });     }    });   },   prev : function(num) {     return this.each(function() {     var el=this;     if(el.spacegalleryCfg.animated==false){      el.spacegalleryCfg.animated=true;      el.spacegalleryCfg.before.apply();      $(el).css('spacegallery', 0).animate({spacegallery:20},{       easing:'easeOut',       duration: el.spacegalleryCfg.duration,       complete: function() {        $(el).find('img:first').appendTo(el).css({"opacity":0}).animate({"opacity":1});        $(el).spacegallery("positionImages");        el.spacegalleryCfg.after.apply();        if(num>1){         num--;         $(el).spacegallery("prev",num);        }        else if((el.spacegalleryCfg.play==true&&num==1)||(el.spacegalleryCfg.play==true&&!num&&!el.spacegalleryCfg.interval)){         el.spacegalleryCfg.interval=setInterval(function() {           $(el).spacegallery("next");          }, el.spacegalleryCfg.pause);        }       },       step: function(now) {        $('img', this).each(function(nr){         var newWidth, top, next;         if (nr == 0) {          top = this.spacegalleryCfg.origTop - ((this.spacegalleryCfg.origTop-this.spacegalleryCfg.prevTop )* now /20);          newWidth = this.spacegalleryCfg.width * top / this.spacegalleryCfg.origTop;          $(this).css({           top: parseInt(top) + 'px',           marginLeft: - parseInt((newWidth-el.spacegalleryCfg.imageWidth)/2, 10) + 'px',           width:parseInt(newWidth)+"px",           opacity:function(){              if(el.spacegalleryCfg.alpha==true){               return 1 -this.spacegalleryCfg.current-now/20;              }              else{               return this.spacegalleryCfg.current-now/20;              }             }          });         } else {          top = this.spacegalleryCfg.origTop -  ((this.spacegalleryCfg.origTop-this.spacegalleryCfg.prevTop )* now /20);          next = this.spacegalleryCfg.current - (this.spacegalleryCfg.current-this.spacegalleryCfg.prev) * now /20;          newWidth =  this.spacegalleryCfg.origWidth - (this.spacegalleryCfg.origWidth - this.spacegalleryCfg.origWidth * el.spacegalleryCfg.minScale)*next;          $(this).css({           top: parseInt(top)+ 'px',           marginLeft: - parseInt((newWidth-el.spacegalleryCfg.imageWidth)/2, 10) + 'px',           width:parseInt(newWidth)+"px",           opacity:function(){              if(el.spacegalleryCfg.alpha==true){               return 1 -next;              }              else{               return 1;              }             }          });         }        });       }      });     }    });   },   to:function(num){    return this.each(function() {     var el=this;     if(num!=el.spacegalleryCfg.images-1){      var dif=0;      if(num<=parseInt((el.spacegalleryCfg.images-1)/2)){       dif=el.spacegalleryCfg.images-(el.spacegalleryCfg.images-1-num);       $(el).spacegallery("prev",dif);      }      else{       dif=el.spacegalleryCfg.images-1-num;       $(el).spacegallery("next",dif);      }     }    });   },   play:function(){    return this.each(function() {     var el=this;     if(el.spacegalleryCfg.play==false){      el.spacegalleryCfg.play=true;      $(el).find(".play").removeClass("pause");      if(el.spacegalleryCfg.animated==false){       el.spacegalleryCfg.interval=setInterval(function() {         $(el).spacegallery("next");        }, el.spacegalleryCfg.pause);      }     }     else{      if(el.spacegalleryCfg.interval){       el.spacegalleryCfg.interval=clearInterval(el.spacegalleryCfg.interval);       el.spacegalleryCfg.play=false;       $(el).find(".play").addClass("pause");      }     }    });   }  };     if ( methods[method] ) {   return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));     } else if ( typeof method === 'object' || ! method ) {   return methods.init.apply( this, arguments );     } else {   $.error( 'Method ' +  method + ' does not exist on jQuery.spacegallery' );     } }; $.extend($.easing,{  easeOut:function (x, t, b, c, d) {  return -c *(t/=d)*(t-2) + b;  } });
Ver más