Raza Mehdi's Blog

Uncategorized

Calculate Difference between two dates in PHP

by on Sep.12, 2012, under Uncategorized

Here is how we can calculate difference between two dates …

$date1 = '2010-11-10';
$date2 = '2012-03-10';

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

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

printf("%d years, %d months, %d days\n", $years, $months, $days);

We can also do this using PHP’s native DateTime & DateInterval objects. I would recommend this method, as this takes care of the leap year checks as well. Remember this is only available in PHP version>=5.3.0

 
$date1 = new DateTime('2010-11-10');
$date2 = new DateTime('2012-03-10');

$interval = $date1->diff($date2);
printf("%d years, %d months, %d days\n", $interval->y, $interval->m, $interval->d);

// Suppose you only want to display the days
printf("difference %d days ",$interval->days);

Leave a Comment more...

How to enable Multisite/Multidomains in WordPress?

by on Sep.12, 2011, under Uncategorized

Hey guys!

I recently worked on a wordpress project, in which i had to enable multiple sites running on a single wordpress installation. Following are steps involved in creating the multi-site on a single wordpress installation:

  • First install wordpress on your machine using virtual host configuration. Suppose, you installed it on the local virtual host with address http://wordpress.localdev/.
  • Edit the wp.config.php and add the following line after /* That’s all, stop editing! Happy blogging. */:
    define('WP_ALLOW_MULTISITE', true);
    
  • Now refresh the wordpress admin page.
  • Again edit the wp.config.phpand add the following lines after /* That’s all, stop editing! Happy blogging. */:
    define( 'MULTISITE', true );
    define( 'SUBDOMAIN_INSTALL', true );
    $base = '/';
    define( 'DOMAIN_CURRENT_SITE', 'wordpress.localdev' );
    define( 'PATH_CURRENT_SITE', '/' );
    define( 'SITE_ID_CURRENT_SITE', 1 );
    define( 'BLOG_ID_CURRENT_SITE', 1 );
    
  • Create/Edit the .htaccessfile, and put the following code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # uploaded files
    RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule . index.php [L]
    
  • Now create the virtual hosts with the address pattern [hostname].wordpress.localdev. All the virtual hosts created using the aforementioned pattern must point to the wordpress installtion folder. Restart apache after this.
  • Now click the link in the top-right column of the admin panel, and select Network Admin. This will bring you in the network admin page:
  • Now click the Sites link in the left-hand column:
  • Now click the link Add New to create a new site:
  • Suppose you created the virtual hosts ads.wordpress.localdev, jobs.wordpress.localdev and coupons.wordpress.localdev. Simply enter ads, jobs and coupons in the site address fields for each site plus the relevant site title and site admin email address.
  • After all sites are created, go to Themes page by clicking the Themes link on the left side:
  • Install any other themes you want. After installing the themes, go to the Themes page to Network enable them.
  • Now click the Site Admin link in the top-right column of the page. This will take you to the main dashboard.
  • Now open a new browser window, and type the virtual host URLs you created above like http://ads.wordpress.localdev/wp-admin to go to admin backend for the respective site. Change the theme by clicking the Appearence link in the left-side column of the admin page. This will change the template for the respective address.
  • When you type in the URL of your virtual host, it will show the frontend with the new active theme for the respective subdomain/virtual host.
  • Also all the users data will be shared across each subdomain setup on wordpress.
Leave a Comment more...

How to create Virtual Hosts using Zend Server CE?

by on Dec.27, 2010, under Uncategorized

Hi guys!

I have been using Zend Server CE as my primary WAMP stack for quite a while, and i really love it. Creating virtual hosts in Zend Server CE is very easy and here is the method for:

  • Suppose the Zend Server installation directory is C:\Zend. Below i will refer to the install path as INSTALLDIR.
  • Open your httpd.conf file located at INSTALLDIR\Apache2\conf in a text editor, and edit the line
    #Include conf/extra/httpd-vhosts.conf

    to

    Include conf/extra/httpd-vhosts.conf
  • Now goto the folder at C:\Windows\System32\drivers\etc and open the hosts file. If you havent yet set the permissions on this file, set it accordingly so that you can edit the file. Add the following lines:
    127.0.0.1          localhost
    127.0.0.1          myapp.localdev
    
  • Open your httpd-vhosts.conf file located at INSTALLDIR\Apache2\conf\extras in a text editor. Uncomment the line (if commented) NameVirtualHost *:80 and put down the following code …
    <VirtualHost *:80>
    DocumentRoot "C:/Zend/Apache2/htdocs"
    ServerName localhost
    </VirtualHost>
    
    <VirtualHost *:80>
    DocumentRoot "C:/Zend/Apache2/htdocs/myapp"
    ServerName myapp.localdev
    <directory "C:/Zend/Apache2/htdocs/myapp">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    </directory>
    </VirtualHost>
  • Now restart the apache webserver. Now, check the urls http://localhost/and http://myapp.localdev/. If no error occurs, then you have successfully configured Virtual Hosting in Zend Server…
4 Comments more...

How to install Diaspora on Windows?

by on Sep.21, 2010, under Uncategorized

So the wait is over. Diaspora is finally here. I have been working on getting it up and running in Windows. After running into some problems over running it, i was finally able to get it working on Windows. So, here are steps to install Diaspora on Windows.

  1. Download the Ruby installer from RubyInstaller download page. Download the installer for Ruby 1.8.7.
  2. When you have installed Ruby, goto command prompt (use as an administrator in Vista/Win7) and navigate your Ruby installation’s directory e.g. [RUBY_INSTALL_DIR]\bin.
  3. Enter the command “gem install rubygems-update”. After successfull installation, again run the command “update_rubygems”.
  4. Download and install MongoDB. Use this guide to install it on Windows. You can also install it as a service as described here.
  5. Download and install OpenSSL from here.
  6. Download and install ImageMagick from here.
  7. Now enter the commands “gem install mongo”, “gem install bson” and “gem install bson_ext” to install MongoDB driver for Ruby.
  8. Now install Ruby DevKit. Installation instructions for this are available from here.
  9. Now open file RUBY_INSTALL_DIR\lib\ruby\gems\1.8\gems\bundler-1.0.0\lib\bundler\lockfile_parser.rb and edit the line 14 to “lockfile.split(/\r?\n+/).each do |line|
  10. Now run command “gem install bundler”. This will bundler gem for Ruby.
  11. Download and install TortoiseGit and msysgit.
  12. Now go to Ruby Install directory. Right click and choose “Git Clone”. This will open up a dialog box. Write “http://github.com/diaspora/diaspora.git” and “C:\RUBY_INSTALL_DIR\diaspora”  as URL and directory respectively and click OK. This will install latest Git repository of Diaspora.
  13. Navigate to the repository folder in command prompt, and run “bundler install”.  This will now install Diaspora.
  14. After a successful installation, run the command “bundle exec thin start“. It may show an error like this: “rbreadline.rb:4404: uninitialized constant RbReadline::Encoding (NameError)“. To remove it, open the file “RUBY_INSTALL_DIR\lib\ruby\site_ruby\1.8\rbreadline.rb” and comment out line 4404. Now run the command “bundle exec thin start” again, and no error will be shown.
  15. Open a browser of your choice, and type in “http://localhost:3000/”. This will open up Diaspora screen like this.
  16. Also note that if you close the command prompt, the server will stop running.
  17. Now close the Diaspora browser window and command prompt.
  18. Open a new command prompt, and navigate to the repository directory. Type in these commands “gem install rake”, “rake db:seed:tom” and finally “bundle exec thin start”.
  19. The commands in the above step will populate some data and will allow you to login with username tom and password evankorth . After entering this username & password, you will see the user homepage.
  20. There it is, a working installation of Diaspora on Windows.
14 Comments more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!