0

Is there any script that can give Equivalent date function in php
Example
If i assign a variable as
26.1.2012

it should output as
26<sup>th</sup> January 2012

Can someone help me on this!

4
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by diafol
2

Yes:

$date = '26.01.2012';
echo date("d\<\s\u\p\>S\<\/\s\u\p\> F Y",strtotime($date));

I've probably overdone the escaping, but it works.

0

easiest way would be tokenize the string and use the tokens. Somewhere you can have array of Months. Also a simple function to check whether to use st, nd, rd or th

0

Yes:

$date = '26.01.2012';
echo date("d\<\s\u\p\>S\<\/\s\u\p\> F Y",strtotime($date));

I've probably overdone the escaping, but it works.

That was easiest than my thought

0

That was easiest than my thought

Yeah really great both!
Thanks a lot!

1

Just another version :P

<?php
$a = array('/st/','/nd/','/rd/','/th/');
echo preg_replace($a,'<sup>$0</sup>',date('dS F Y'));
?>
1

Just for completeness, I tried stripping as many backslashes as poss.:

$date = '26.01.2012';
echo date("d<\s\up>S</\s\up> F Y",strtotime($date));

I like cereal's a lot, but it messes up if you have other strings that contain 'rd' like Saturday in this:

echo preg_replace($a,'<sup>$0</sup>',date('l dS F Y',strtotime('28.01.2012')));

Changing to this does make it work though:

$a = array('/[d]st/','/[d]nd/','/[d]rd/','/[d]th/');
$date = '28.01.2012';
echo preg_replace($a,'<sup>$0</sup>',date('1 dS F Y',strtotime($date)));

Edited by diafol: n/a

Votes + Comments
perfect! :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.