Tuesday, November 11, 2008

Explode Function

So here we go for the 1st post in this blog.

Explode function, i think many of us already know of this function.
Explode is very useful function, when it comes to breaking up lines or words.
There are several ways to use this function.
I will give some examples, that how to use it.

lets assume that you stored datetime in database as datetime. the way your date looks is:
2008-11-11 12:25:20

But now you want, to breakup the date and time into two parts.
So lets break it:
$date = "2008-11-11 12:25:20";
$datetime = explode(" ",$date);

The above code already break your datetime into 2 parts now lets echo it as it is in array.
echo $datetime[0]; For date
echo $datetime[1]; For time

The 1st echo will give you just date: 2008-11-11
The 2nd echo will print just time: 12:25:20

You can break this into more parts, if you want, like seconds, minutes, hours and same with date.
For example lets break time.
$time = explode(":",$datetime[1]);
echo $time[0]; For hours
echo $time[1]; For minutes
echo $time[2]; For seconds

I will also extend this to the other post later "Calculating Unix time in PHP (part-1)". which is here: Link

Regards
ANL

No comments: