I-M-JM
I-M-JM

Reputation: 15950

PHP formatting date, without using date functions

I am getting date in following format, which is a string:

31_Dec_2010

Now, I need to convert it to following 2 format

2010-12-31 //(for use in mysql query)

and

31 Dec, 2010 //(for displaying purpose)

I cannot use Date-Time Functions because I am running code on PHP 4.8

Please suggest me some way, other then "explode" function

Upvotes: 1

Views: 128

Answers (2)

TJHeuvel
TJHeuvel

Reputation: 12608

According to the manual PHP's date functions have been available since version 4. Assuming you cant update (for whatever reason), you still should be able to use them.

You should reconsider using explode/split, you can safely split on _ to get each part of the date. The only tedious part of that is to convert the month to the numeric value, MySQL might have some handy syntax for that too.

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382726

You can do like this:

echo date('Y-m-d', strtotime(str_replace('_', '-', '31_Dec_2010')));

Output:

2010-12-31

I cannot use Date-Time Functions because I am running code on PHP 4.8

These functions are available in the version of PHP you are using :)

Upvotes: 5

Related Questions