Search This Blog

Tuesday, March 27, 2012

Making a mysql back up every day midnight using cron

This is a simple way to back up your database everyday Go to ubuntu command line and log in as root user, type following command to open crontab file

 >crontab -e

 type the following command in the file

 0 0 * * * mysqldump -uYOURUSER -pYOURPASSWORD YOURDBNAME > /home/tommy/my_back_up/mydb_`date +\%y-\%m-\%d`.sql

 you can restart cron by typing

 >restart cron

 OR

 >/etc/init.d/cron restart

This will create a sql dump file everyday midnight
file name would be mydb_2012-04-21.sql

Wednesday, March 7, 2012


/**
     * Converts minutes to hours
     * @param type $mins
     * @return string 
     */
    public static function m2h($mins) {
        if ($mins < 0) {
            $min = Abs($mins);
        } else {
            $min = $mins;
        }
        $H = Floor($min / 60);
        $M = ($min - ($H * 60)) / 100;
        $hours = $H + $M;
        if ($mins < 0) {
            $hours = $hours * (-1);
        }
        $expl = explode(".", $hours);
        $H = $expl[0];
        if (empty($expl[1])) {
            $expl[1] = 00;
        }
        $M = $expl[1];
        if (strlen($M) < 2) {
            $M = $M . 0;
        }
        $hours = $H . "." . $M;
        return $hours;
    }

Tuesday, March 6, 2012

PHP function to return an array representation of calender month

If you need an array representation of a calender month this function will come in handy. This function will return an array of a given month with days properly distributed in to weeks, as in real calendar.

public function buildMonthCalendar($year, $month) {

        $calendar = array(
            'week-1' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-2' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-3' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-4' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-5' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
            'week-6' => array('Mon' => null, 'Tue' => null, 'Wed' => null, 'Thu' => null, 'Fri' => null, 'Sat' => null, 'Sun' => null),
        );

        $startOfMonth = "{$year}-{$month}-01";
        $result = strtotime("{$year}-{$month}-01");
        $endOfMonth =  date('Y-m-d', strtotime( date('Y-m-d', strtotime( $result . '+1 month')).' -1 second'));

        $j = 1;
        for ($i = $startOfMonth; $i != $endOfMonth;) {

            $weekDay = date('D', strtotime($i));
            $calendar["week-{$j}"][$weekDay] = date('d', strtotime($i));
            if ($weekDay == 'Sun') {
                $j++; //jump to next week
            }
            $i = date('Y-m-d', strtotime($i . ' +1 day'));
        }

        $weekDay = date('D', strtotime($endOfMonth)); //last day of the month
        $calendar["week-{$j}"][$weekDay] = date('d', strtotime($endOfMonth));

        return $calendar;
    }


Change color of table column using jquery

If the table id is punchData and if you are changing second column color
$("#punchData > tbody > tr > td:nth-child(2)").css("background","#F5D0A9");