PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 07, 2020 11:38 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jul 14, 2005 2:33 pm 
Offline
Spockulator
User avatar

Joined: Wed Feb 04, 2004 9:15 pm
Posts: 4714
Location: Eden, Utah
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
Syntax: [ Download ] [ Hide ]
<?

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

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.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 3:28 pm 
Offline
Jedi Mod
User avatar

Joined: Tue Dec 21, 2004 6:03 pm
Posts: 5263
Location: usrlab.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 5:55 pm 
Offline
Spockulator
User avatar

Joined: Wed Feb 04, 2004 9:15 pm
Posts: 4714
Location: Eden, Utah
good idea, I also fixed a few bugs that were stupid oversights on my part.

done and done.

thx,

Burrito
8O


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group