Social Icons

Tuesday, April 8, 2014

Calculate difference between two dates in php

Suppose you have two dates as some start date and end date. You have to find the difference between those dates in years, months and days. It can be easily done by converting the date into timestamp value using strtotime function.

Say, you have two dates as follows:

$date1 = "2012-04-24";
$date2 = "2013-05-28";

Convert these dates into timestamp and subtract these.

$difference = abs(strtotime($date2) - strtotime($date1));

Now, you can calculate difference in years, month and days as follows:-

$years = floor($difference / (365*60*60*24));
$months = floor(($difference - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($difference - $years * 365*60*60*24 - $months * 30*60*60*24) / (60*60*24));

Another way to do it using OOP approach as follows:-

$date1 = new DateTime("2012-04-24");
$date2 = new DateTime("2013-05-28");

$interval = $date1->diff($date2);
echo $interval->y ." years ".$interval->m." months ".$interval->d." days";

Thursday, March 20, 2014

Why you shouldn't use mysql_* functions in php?

To use php with mysql database, there are many php functions for it. For example: mysql_connect(), mysql_select_db(), mysql_query(), mysql_real_escape_string() etc. Now you need to move away from it and stop using these functions. 

Very straight forward answer to stop using these functions is that they are outdated. These functions ae likely to be deprecated in higher versions of php. Some important reasons to avoid these can be concluded as below: 
  • They are likely to be deprecated.
  • All Mysql 5.1 functionalities are not supported.
  • They don't support stored procedures.
  • They don't support multiple statements etc.

You can start using mysqli or PDO extensions APIs to do the database sort of things.

Friday, November 22, 2013

Add Contact Form in Blogger

Contact form can be an essential requirement to be there in the blog. Here, I am going to tell you how to add a contact form in blogger on a specific page or in sidebar.

You can add the contact form either on sidebar or on a specific page. Its preferred to add it in a specific different page. Anyway I am going to tell you for both things. Follow the below steps: 

  • Go to Layout of your blog.
  • Click on "Add Gadget" link there and find out for "Contact Form" page element. It can be either there or in "More Gadgets". If you can to edit the title from "Contact Form" to something else, then you can do that too by editing the text box there.
  • Now, save the arrangement and you can see a contact form in your blog.
But what if you want the contact form (you created above) in a specific page. For this, follow the below steps: 
  • Let it be same as you added a gadget of  "Contact Form" in above steps.
  • Now, you would like to remove this gadget from the sidebar as you want it in a specific page. So, hide it first. Go to "Template" and click on "Edit HTML" button. Then paste the below code just above ]]></b:skin>

    #ContactForm1
    {
    display:none ! important;
    }
     


    Here, what we are doing is that hiding the contact form from sidebar with help of css.
  • Now, we have to add the contact form in a specific page. Go to pages, click on "New Page" with blank page template.
  • Switch to html mode,.
  • Paste the below html code there:
    <form name="contact-form">
    <p></p>
    Name: <br />
    <input class="contact-form-name" id="ContactForm1_contact-form-name" name="name" size="30" value="" type="text" />
    <p></p>Email: 
    <span style="font-weight: bolder;color:red;">*</span><br />
    <input class="contact-form-email" id="ContactForm1_contact-form-email" name="email" size="30" value="" type="text" />
    <p></p>
    Message: <span style="font-weight: bolder;color:red;">*</span><br />
    <textarea class="contact-form-email-message"  id="ContactForm1_contact-form-email-message" name="email-message" cols="25" rows="5"></textarea>
    <p></p>
    <input class="contact-form-button contact-form-button-submit" id="ContactForm1_contact-form-submit" value="Send" type="button" />
    <p></p>
    <div style="text-align: center; max-width: 222px; width: 100%">
    <p class="contact-form-error-message" id="ContactForm1_contact-form-error-message"></p>
    <p class="contact-form-success-message" id="ContactForm1_contact-form-success-message"></p>
    </div>
    </form>
  • This will create a form there with the fields of Name, Email, Message and a Send button. User will enter his/her name, email address and message and click on send button which will be delivered to your email address, you registered to create the blog. Thats it.

Troubleshoot if your contact form is not working for your blog

If on doing the above steps, still your contact from doesn't work properly, then follow the below steps to troubleshoot it: 
  • Go to your "Template" page and click on "Edit HTML" button.
  • There will be a "Jump to Widget" drop down button. Click on that and it will give you a list. Select "ContactForm1" id. Accordingly, some code will be displayed there. Just find out the below code:

    <form name='contact-form'>
            <p/>
            <data:contactFormNameMsg/>
            <br/>
            <input class='contact-form-name' expr:id='data:widget.instanceId + &quot;_contact-form-name&quot;' name='name' size='30' type='text' value=''/>
            <p/>
            <data:contactFormEmailMsg/> <span style='font-weight: bolder;'>*</span>
            <br/>
            <input class='contact-form-email' expr:id='data:widget.instanceId + &quot;_contact-form-email&quot;' name='email' size='30' type='text' value=''/>
            <p/>
            <data:contactFormMessageMsg/> <span style='font-weight: bolder;'>*</span>
            <br/>
            <textarea class='contact-form-email-message' cols='25' expr:id='data:widget.instanceId + &quot;_contact-form-email-message&quot;' name='email-message' rows='5'/>
            <p/>
            <input class='contact-form-button contact-form-button-submit' expr:id='data:widget.instanceId + &quot;_contact-form-submit&quot;' expr:value='data:contactFormSendMsg' type='button'/>
            <p/>
            <div style='text-align: center; max-width: 222px; width: 100%'>
              <p class='contact-form-error-message' expr:id='data:widget.instanceId + &quot;_contact-form-error-message&quot;'/>
              <p class='contact-form-success-message' expr:id='data:widget.instanceId + &quot;_contact-form-success-message&quot;'/>
            </div>
          </form>
  • Just remove the above code and you are done. Now check your blog again. The contact form should be working fine now.

Sunday, October 20, 2013

Call to a member function set_userdata() on a non-object: Codeigniter

Once I was using sessions in codeigniter and got the above error "Call to a member function set_userdata() on a non-object. Later I fixed it and providing the same solution for it. 

This error comes in codeigniter if you haven't loaded session library in your file or project. You can do it in two ways. You can either load the session library in the file itself or autoload session library in autoload.php

Load the session library in the file as follows: 
$this->load->library('session');

Or you can autoload session library as follows: 
$autoload['libraries'] = array('database', 'session');

This should fix your error.

In order to use the Session class you are required to set an encryption key in your config file

This error code belongs to codeigniter (PHP MVC framework). Once I upgraded my codeigniter and while using session in codeigniter, I got the error as : "In order to use the session class you are required to set an encryption key in your config file". Here, I am providing the solution for this error. I fixed it as follows:

The reason they ask for an encryption key is security. Even if you are not using encrypted sessions, you must set an encryption key in your config file which is used to aid in preventing session data manipulation. So, you have to change in config.php as:

$config['encryption_key'] = 'your_encryption_key_here';

This should fix your error.

Tuesday, October 8, 2013

Maximum execution time of 60 seconds exceeded: Magento

Once I was in the stage of installing magento on my machine and got this error: maximum execution time of 60 seconds exceeded. And then later I fixed it. I am just sharing the thing that I do and is required to fix the above error.

Open your php.ini configuration file (It will be inside php folder). Search for max_execution_time there in the file. Assign it the value of 1800. So, it will be now max_execution_time = 1800. Similarly, just below that there will be max_input_time. Assign it also the same value as 1800. Save the file and restart your wamp or xampp server whatever you are using. That should fix the error.

Wednesday, September 18, 2013

Unsupported template dependency: Upgrade your android eclipse plugin

Once I was working on creating an android application and while configuring, I got this error as "Unsupported template dependency: Upgrade your android eclipse plugin". Below it was written as :
Required version: (empty)
Installed Version: 18

Here, I got the fix for this issue.
Suppose if you selected for Blank Activity then you have to make one change as follows:
Go to sdk/tools/templates/activities/BlankActivity/template.xml
and comment out the dependency tag as follows:
<!--<dependency name="appcompat" version="v7" />-->

Thats it. You are done. Restart eclipse and try to create again. This fixed my issue.

Similarly, if you selected for MasterDetailFlow then
Go to sdk/tools/templates/activities/MasterDetailFlow/template.xml and comment the line
<!--<dependency name="support" version="v4" />
<dependency name="android-support-v4" revision="8"/>-->

Thats it. Above steps should fix your issue of unsupported template dependency.

Total Pageviews