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!

Member Avatar for diafol

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.

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

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

That was easiest than my thought

Yeah really great both!
Thanks a lot!

Just another version :P

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

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)));
commented: perfect! :) +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.