dates

jQuery tablesorter custom date sort

I needed a custom sort column for jquery tablesorter (http://tablesorter.com/docs/) for dates in the format of 12-Aug-2013 as the default tried to sort it as a string

Was quite complicated to work out, but think its done:

$.tablesorter.addParser({
    'id' : 'customDates',
    'is' : function(string) {
         return false;
         },
    'format' : function(string) {
        if (string == "") {
            return '';
        }
        var thedate = string.split('-');
        var monthint = {};
        monthint['Jan'] = "01";
        monthint['Feb'] = "02";
        monthint['Mar'] = "03"; 
        monthint['Apr'] = "04";
        monthint['May'] = "05";
        monthint['Jun'] = "06";
        monthint['Jul'] = "07";
        monthint['Aug'] = "08";
        monthint['Sep'] = "09";
        monthint['Oct'] = "10";
        monthint['Nov'] = "11";
        monthint['Dec'] = "12";
        var date_day = parseInt(String(thedate[0]));
        if (date_day.length == 1) {
            date_day = '0' + date_day;
        }
        var date_month = monthint[thedate[1]];
        var date_year = thedate[2];
        return date_year + date_month + date_day;
    },
    'type' : 'numeric'
    });

And then apply it to the table

$('table').tablesorter({
    'debug' : 'true', 
    'headers' : {
        0 : {
            'sorter' : 'customDates'
            } 
        } 
    });

Zend_Date – difference between 2 dates

I keep having trouble with this simple calculation! So, here is a little example to get the amount of days between now and the next occurance of July 1st.

//set up the 2 date objects
$now = new Zend_Date();
$next = new Zend_Date();
//we want July 1st
$next->setDay(1)->setMonth(7);
//if we are currently after july this year
if ($now->get(Zend_Date::MONTH) >= 7) {
//we need to work it out til next July
$next->setYear($now->get(Zend_Date::YEAR) + 1);
}
else {
//this july is fine
$next->setYear($now->get(Zend_Date::YEAR));
}
$diff = $next->sub($now)->toValue();
return $diff / 60 / 60 / 24;