954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Equivalent date function in php

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
26th January 2012

Can someone help me on this!

jhbalaji
Light Poster
45 posts since Mar 2009
Reputation Points: 10
Solved Threads: 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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
That was easiest than my thought


Yeah really great both!
Thanks a lot!

jhbalaji
Light Poster
45 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Just another version :P

<?php
$a = array('/st/','/nd/','/rd/','/th/');
echo preg_replace($a,'<sup>$0</sup>',date('dS F Y'));
?>
cereal
Master Poster
710 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

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)));
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You