Saturday, December 14, 2013

Find and Replace with Regular Expressions in PHP

If we want to remove a specific part from a string, we can use regular expressions in php

<?php $oldstring="12. What is Your Name";
$newstring=preg_replace('/[\d]+[\.]+[\s?]/', '',$oldstring);
echo $newstring;

Result : What is Your Name
Here removes serial number from a string. eg : 12. Pattern match search for [\d]+[\.] . This means any digit followed by a dot(.)

Wednesday, August 21, 2013

Age Calculation in MySQL based on Date of Birth Field

To find out age in mysql we can use two methods. (Assume that DOB is the field name in table)

1)SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(DOB, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(DOB, '00-%m-%d')) AS age FROM students

The above query returns the exact age. (looking for birth date)

- 2013-08-22 - 2008-11-02  returns 4 

2) SELECT YEAR( CURDATE( ) ) - YEAR( DOB ) AS age FROM students.

The above query returns the age difference by year (looking for year only)

- 2013-08-22 - 2008-11-02  returns 5

Monday, August 5, 2013

MySQL Command Line Backup

From Shell prompt Enter the following.
 $ mysqldump -u root -pmqr dbname |gzip -9 > /backup/koha/dbname-$(date +"%d-%m-%Y-%H:%M:%S").gz

This command will make mysql backup and compress it in the name format  dbname-06-08-2013-17:17:34.gz