Converting to and from epoch time

Epoch time is Unix timestamp which counts the number of seconds since January 1 1970 00:00:00 UTC. Without getting into long discussion, follow is how your convert between human readable date and epoch time.

Convert Human Readable Date to Epoch Time

  $now = strtotime('2010-04-25');
  print $now;

Convert Epoch Time to Human Readable Time

  $human_date=date("Y-m-d", 1251627495);
  print human_date;

Validating Email in PHP

Validating email is more difficult than one might think. Recently I found this this code at and I feel a need to republish this to emphasize its effectiveness and simplicity.

$email = "";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "E-mail is not valid";
} else {
  echo "E-mail is valid";

PHP Array functions

Passing arrays by value and reference

The difference between passing by value and passing by reference is that you when pass by value, you are passing a copy of the array. When passing by reference, you are passing a pointer i.e any changes made in the function you pass to can modify the original.

function foo($var) {
function bar(&$var) {

print "Passed by value:\n";
print "Passed by reference:\n";


PHP File functions

Opening directory and listing files

See the following code:

  $dir = 'mydir';
  $myfiles = scandir($dir);


  [0] => .
  [1] => ..
  [2] => one.txt
  [3] => two.txt

To remove certain file name from the list, see the following code:

Installing PHP on Mac OS

Installing PHP is more than just installing the PHP source code on the system. Several libraries are also required to connect to databases, connect to GD, support XML, get LDAP access and so on. All these have to be installed manually on the machine. You can go through the painful process of manual installation and configuration or simply download and install a ready-made package from

Unless you have very specific needs, you should use this package.

PHP fatal error - call to undefined function mb_string

This error tells us that the mb_string library is not installed. mb_String mb_String is a library, which provides support for UTF-8 and UCS-2 characters. PHP uses ASCII by default which is a 256 character coding system. This means that it uses 8 bits to encode English alphabet and commonly used symbols such as ,.?;:"+-$#@ and many more. However, it does not code for foreign language characters such as é, ç, â, è, ñ, ü, and many others. mb_string provides double byte, i.e. 2 x 8bits to code for a character.

Encrypting and decrypting in PHP

The crypt() function provide one-way encryption. Using one-way encryption is like using a key to lock and unlock something. The key is your password.

To encrypt:

$pass = 'secret';
$encrypted = crypt($pass);
// $encrypted = $1$zaxz8vXb$.lZaoK40w/EtrkkogORYo0

To decrypt:

// entered password
$pass = 'secret';
// $password is the encrypted password
if (crypt($pass, $encrypted) == $encrypted) {
  print 'welcome';
} else {
  print 'wrong password';