The right way to convert a date string to Mktime

When we do start with PHP is quite common to use a series of functions to transform a date string into Unix time format, but there is an easier and faster way to do it.

Not so correct way:

$date           = ‘2010-09-03’;
$dateArray = explode (‘-‘, $date);
$unixTime  = mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]);

This will work 100% it uses 2 functions to return the correct result. If the system need’s to run this into 10,000,000 results it will take a considerable more time than the second option bellow.

The correct way:

$date           = ‘2010-09-03’;
$unixTime = strtotime($date);

Simple and effective.

Have fun.

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: