Calculate age in years months and days in mysql

When we are dealing with dates in mysql sometimes we need to calculate the age in years months and days directly from the mysql query

We can easily achieve it from the below sql query


SELECT name,dob,
CONCAT(TIMESTAMPDIFF( YEAR, dob, now() ),' Years,',
TIMESTAMPDIFF( MONTH, dob, now() ) % 12,' Months,',
FLOOR( TIMESTAMPDIFF( DAY, dob, now() ) % 30.4375 ),' Days')
as AGE from users

And the output of the query will shown like this


+-----------------+------------+-----------------------------+
| name            | dob        |  Age                        |     
+-----------------+------------+-----------------------------+
| Arun            | 1989-09-07 | 25 Years, 6 Months, 8 Days  |
| Disilva         | 1960-12-04 | 54 Years, 3 Months, 11 Days |
| Pam             | 1990-07-03 | 24 Years, 8 Months, 13 Days |
+-----------------+------------+-----------------------------+

OpenID 2.0 for Google Accounts is going to Shutdown

Google OpenID 2.0 for Google Accounts is going to Shutdown and they are migrating Open ID 2.0 to the Google Plus Sign-In. Open ID 2.0 and Google Plus Sign-In is a way that allows website owners to let their users log into their website using their Google account credentials.

Shutting down Open ID 2.0 and migrating to Google Plus Sign-In is further proof that Google Plus is really here to stay.

According to Google, there is no Developer Registration for Open ID 2.0, so there is no way for Google to contact web developers using Open ID 2.0. However, I believe that there would most likely be a way for Google to identify the sites using Open ID 2.0. For example, they could actually identify the sites using it–and then contact the site owners using Google Webmaster Tools.

Here is the schedule for the migration to Google Plus Sign-In:

January 12, 2015: Auto-approval is progressivly disabled for more and more users, these users will be forced to re-consent. Developers passing openid_shutdown_ack are unaffected.

February 23, 2015: Auto-approval is turned off, users must consent on each request. Developers passing openid_shutdown_ack are unaffected.

March 23, 2015: Grace period for developers using the openid_shutdown_ack parameter ends, warning is shown and auto-approval turned off for all applications.

April 20, 2015: OpenID is shut down. A static error page will be displayed on all requests. Make sure you have migrated well before this date.

Import CSV File Into MySQL using LOAD DATA INFILE

In this post we are discussing on how to use LOAD DATA INFILE statement to import CSV file into MySQL table.

The following statement imports data from c:\tmp\data.csv file into the discounts table.

LOAD DATA INFILE 'c:/tmp/data.csv' 
INTO TABLE discounts 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

The field of the file is terminated by a comma indicated by FIELD TERMINATED BY ‘,’ and enclosed by double quotation marks specified by ENCLOSED BY ‘”‘.

Each line of the CSV file is terminated by a new line character indicated by LINES TERMINATED BY ‘\n’.

Because the file has the first line that contains the column headings, which should not be imported into the table, therefore we ignore it by specifying IGNORE 1 ROWS option.

Transforming data while importing

Sometimes the format of the data does not match with the target columns in the table. In simple cases, you can transform it by using the SET clause in the LOAD DATA INFILE statement.

Suppose the date column in the data.csv file is in mm/dd/yyyy format.

When importing data into the discounts table, we have to transform it into MySQL date format by using str_to_date() function as follows:

LOAD DATA INFILE 'c:/tmp/data.csv'
INTO TABLE discounts
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(title,@date,amount)
SET date= STR_TO_DATE(@date, '%m/%d/%Y');

Hope this will help to import data from csv to mysql database

Add Fields To Custom Taxonomy

As a WordPress developer, some times we need to add fields to custom taxonomy. So, in this post we are going through the code snippet for adding field to taxonomy. Let have a look,

Here we are taking the taxonomy named `product_cat` which is the woocommerce product category and we are adding the field to feature product categories in woocommerce

/** Add Custom Field To Category Form */
add_action( 'product_cat_add_form_fields', 'product_cat_taxonomy_custom_fields', 10, 2 );
add_action( 'product_cat_edit_form_fields', 'product_cat_taxonomy_custom_fields', 10, 2 );
function product_cat_taxonomy_custom_fields($tag) {  
   // Check for existing taxonomy meta for the term you're editing  
   $t_id = $tag->term_id; // Get the ID of the term you're editing  
   $term_meta = get_option( "product_cat_featured_$t_id" ); // Do the check  
?>  
  
<tr class="form-field">  
    <th scope="row" valign="top">  
        <label for="presenter_id"><?php _e('Featured'); ?></label>  
    </th>  
    <td>  
      <select name="featured" id="featured" class="postform">
	<option value="0">Select</option>
	<option <?= $term_meta=='Yes'?'selected':'' ?>   value="Yes">Yes</option>
	<option <?= $term_meta=='No'?'selected':'' ?>   value="No">No</option> 
</select> 
    </td>  
</tr>  
  
<?php  
}  

The above code allows us to add out html code to the add new page and edit taxonomy page
we can modify to this to any html you need.
Add Fields To Custom Taxonomy
The only thing to note here is to keep the name of the field we created and we need that to save the data to the database

Now the second part of the code is to save the data to the database

/** Save Custom Field Of Category Form */
add_action( 'created_product_cat', 'product_cat_form_custom_field_save', 10, 2 );	
add_action( 'edited_product_cat', 'product_cat_form_custom_field_save', 10, 2 );
 
function product_cat_form_custom_field_save( $term_id, $tt_id ) {
 
	if ( isset( $_POST['featured'] ) ) {			
		$option_name = 'product_cat_featured_' . $term_id;
		update_option( $option_name, $_POST['featured'] );
	}
}

Hope this will help you to add fields to custom taxonomy and you can easily modify this code to you taxonomy
the only thing you need to change is `product_cat` and the name of the field