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...
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>thx to D11 for some regex help, and thx to hawleyjr for some inspiration...