English language date formatter

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

English language date formatter

Post by Burrito »

I'm not sure how many times I've opened my .chm file for the manual to read the date function parameters...too many to count on all of my body parts.

as a result, I decided to whip something up that I can use with just plain 'ole english to format my dates.

it's in a class right now which obviously isn't necessary *at all*, but I was trying to kill two birds with one stone (get a better understanding of OOP is the little dove bird that I killed with the stone).

it still needs a lot of work to account for different options and I haven't even dealt with time stuff yet, but here is the date part.

If people are interested, I will beef it up some more and repost here... 8O

see it working here

Code: Select all

<?
class datesFormat{
	var $dates;
	function datesFormat($dates){
		$dates = preg_replace("#[/-\s\.]#","/",$dates);
		$dates = date("Y-m-d", strtotime($dates));
		$this->dates = $dates;
	}
	function formatDates($sorted="m/d/Y"){
		static $twoyear = array("/(two|2)\s*digit\s*year/i","/short\s*year/i","/year\s*with\s*(two|2)\s*digits/i","/y/");
		static $abvday = array("/short\s*day/i","/ab[a-z]* day/i","/D/","/weekday\s*ab[a-z]*/i","/ab[a-z]*\s*weekday/i");
		static $fullday = array("/Full\s*Day/i","/day\s*spelled\s*out/i","/day\s*of\s*[a-z]*\s* week/","/(weekday)[^a-z]*/i","/l/");		
		static $shortmonth = array("/short\s*month/i","/ab[a-z]* month/i","/month\s*ab[a-z]*/i","/M/");
		
		static $daysinmonth = array("/(last|final).*?month/i","/month('|\s|\w)*(last|final).*?day/i","/number[\s\w]*month/i","/days[\s\w]*month/i","/t/");
		static $shortday = array("/number\s*day/i","/d/","/day\s*(?!no)(?:[\w\s](?!no))*zero/i");
		static $shortdaynozero = array("/day\s*without\s*zero/i","/j/","/day(\s|\w)*no(\s|\w)*zero/i");
		static $fullmonth = array("/Full\s*month/i","/month\s*spelled\s*out/i","/F/");
		static $fullyear = array("/full\s*year/i","/Y/","/(four|4)\s*digit\s*year/i");
		static $shortmonthd = array("/number\s*month/i","/m/","/month\s*(?!no)(?:[\w\s](?!no))*zero/i");
		static $shortmonthnozero = array("/month\s*without\s*zero/i","/n/","/month(\s|\w)*no(\s|\w)*zero/i");
		static $dayofyear = array("/day(\s|\w)*year/i","/z/");
		
		$sorted = preg_replace($twoyear,"y",$sorted);
		$sorted = preg_replace($abvday,"D",$sorted);	
		$sorted = preg_replace($fullday,"l",$sorted);
		$sorted = preg_replace($shortmonth,"M",$sorted);
		$sorted = preg_replace($daysinmonth,"t",$sorted);
		$sorted = preg_replace($shortday,"d",$sorted);
		$sorted = preg_replace($shortdaynozero,"j",$sorted);
		$sorted = preg_replace($fullmonth,"F",$sorted);	
		$sorted = preg_replace($fullyear,"Y",$sorted);
		$sorted = preg_replace($shortmonthd,"m",$sorted);
		$sorted = preg_replace($shortmonthnozero,"n",$sorted);
		$sorted = preg_replace($dayofyear,"z",$sorted);
		if(preg_match("/mysql/i",$sorted))
			return $this->dates;
		else 
			return date($sorted, strtotime($this->dates));
			
	}
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>English Date Formatter</title>
</head>

<body>
<form method="post">
Enter A Date (mm/dd/yyyy): <input type="text" name="date" <?=isset($_POST['date']) ? "value=\"".$_POST['date']."\"" : "";?>><br>
Enter A Date Pattern: <input type="text" name="pattern" size="50" <?=isset($_POST['date']) ? "value=\"".$_POST['pattern']."\"" : "";?>><br>
(examples "Day spelled out, 2 digit year, fullmonth, last day of the month, fullyear shortmonth, month abbreviated, abvr month" etc)<br>
<input type="submit" value="go">

</form>
<?
if(isset($_POST['date'])){
	$thisone = $_POST['pattern'];
	$date = new datesFormat($_POST['date']);
	$showdate = $date->formatDates($thisone);
	echo "Result: <b>$showdate</b>";

echo "<br><br>Pattern Used: <b>$thisone</b>";
}
?>


</body>
</html>
see it working here

thx to D11 for some regex help, and thx to hawleyjr for some inspiration...
Last edited by Burrito on Thu Jul 14, 2005 6:30 pm, edited 2 times in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Clever.

Shouldn't you declare the various arrays in the function as static though? That'd save them being rebuilt if the function is called several times in the same script.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

good idea, I also fixed a few bugs that were stupid oversights on my part.

done and done.

thx,

Burrito
8O
Post Reply