SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    528
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Parsing english dates

    Is there a nice, flexible way of getting a timestamp for an english date ie dd/mm/YY
    strtotime is a great function in that you don't need to know exactly what format the input is in. If only there were some way of doing the same for english format dates??

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $str='12/11/2007';
     
    $date=explode('/',$str);
     
    echo date('Y m d',mktime(0,0,0,$date[1],$date[0],$date[2])); //2007 11 12
    Saul

  3. #3
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    528
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    yes but then you'd need to know how the date is delimited. And include extra options if you wanted to (optionally) include the time or day of week elements.
    Basically, before I write some big pettern matching extravaganza, I was wondering if I'm missing something that's already been done?

  4. #4
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    528
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Most common case is that the date has come from user input, and I'll probably convert it into the correct format and maybe to put it into the database as datetime type.

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, then perhaps something along the lines:

    Code php:
    $str='12/11/2007';
     
    $date=preg_split('#[-/. ]#',$str);
     
    print_r($date);
     
    echo date('Y m d',strtotime($date[1].'/'.$date[0].'/'.$date[2])); //2007 11 12

    That is switching the month/day pair places.
    Saul

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Where is the english date coming from and exactly what do you want to do with it next?

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    User inputs can be a big problem - I think your best option is to provide drop-down boxes, or some other form of graphical format.

    Maybe 2 textboxes, but the time format is specified by the label, e.g.:
    Code:
    Date (mm/dd/yy): [__]/[__]/[____]
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    528
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What I have is a nice little javascript date selector which allows the user to select a date and pops it in to the text box in the correct format.

    But obviously you need to account for non-js users. Personally I like to keep the date input to 1 form element, and validate it.

    I basically wondered if I was missing some super date-parsing function. I'll go work on some validation/flexible pattern matching, and post back.

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match_all('~\d+~'$str$m);
    $m $m[0] + array_fill(060);
    $time mktime($m[3], $m[4], $m[5], $m[1], $m[0], $m[2]); 
    This will parse "11/12/2007" and something like "11.12.7 2h 15min" as well.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    >But obviously you need to account for non-js users. Personally I like to keep the date input to 1 form element, and validate it.

    That's a mistake in my view. Users will inevitably get it wrong.

    It depends how much effort you want to put into getting this right for the user.

    A US citizen living in the UK will give you duff data, like 1/2/2008 which you will go ahead and parse as 1st Feb, when she means 2nd Jan.

    Its a valid date.

    As for your gui, a nice no-script option is simply to write 3 date boxes using range() with correctly marked labels. eg

    PHP Code:
    echo '<noscript>';
    echo 
    '<select name='day' id='day'>';
    $days range1,31);

    foreach( 
    $days as $d){
    echo 
    '<option value='.$d.'>'.$d.'</option>';
    }

    echo 
    '</select>';
    // and so on for the month and year

    echo '</noscript>' 

    So what you are left with is a non-javascript user who enters a non-existent date ( like 31st of Feb ) so you run the result through the PHP function checkdate().

    Incidentally you could run checkdate() over all incoming dates, even results from your JS enabled users, so you don't enter bad data in your dbase.

    Why would you want to checkdate() dates from your JS calendar? Because users can change this data at will - something you would want to check for and snag before you go off doing anything else with the submitted data.

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    There are so few users who dont have javascript enabled in the real world that it's not a big worry really, yes offer an alternative but dont base everything on it.

    The less chance the user has of incorrect input the better.
    Give them the three dropdowns with really big labels and maybe visual hints like tootips.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Hi,

    I tend to agree with Spike, though hessodreamy mentioned non-js - and I didn't spell it out implicitly, but you could use a range-type generator or just plain hard-coded html select boxes instead of JS.

    (The clever thing with JS is that you are assured that the input boxes, when you select Feb for example will only create 28 (or 29) days)

    But either way you still you need to check the date is a real one.

    If date handling is new to you, then I'd say this method is a pretty simple way to go - sidestepping the onerous and sometimes bug-ridden task of exploding, rebuilding and verifying dates entered via freeform text boxes.

    HTH

    Paul

  13. #13
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    528
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would tend to agree with you regarding the use of selects, for a public website. You can't trust the user to not be a thicko or, on the other hand, an american () and enter the date incorrectly. I like cups' dual method of using selects for those without js.

    However what I'm working on right now is a back-office admin page so it means 1) I can rely a little more on the user 2) They're doing repetitive jobs so quick and easy is the way (ie not having a row of 3 selects).

    But such usability talk is a bit off topic.

    Regarding the parsing of dates. When I come down to it, all I'm doing is 2 things:
    1. getting a datetime from the database and displaying it in some format
    2. Getting some user-entered date and putting it in the database as a datetime

    To this end I just came up with 2 pretty simple functions to do this:
    Code:
    function date_to_db($dateStr)
    {
    	//split it
    	$ukDatePattern="/^([0-9]{1,2})[\/\.,\- ]([0-9]{1,2})[\/\.,\- ]([0-9]{2,4})( ([0-9]{1,2}) ?: ?([0-9]{1,2})( ?: ?([0-9]{1,2}))?)?$/";
    	if(!preg_match_all($ukDatePattern, $dateStr, $matches)) return false;
    	if(count($matches)<3) return false;
    	$d=$matches[1][0];
    	$m=$matches[2][0];
    	$y=$matches[3][0];
    	
    	//check parts
    	if(!checkdate($m,$d,$y)) return false;
    	
    	//stick it back together
    	$dbDate=$y."-".$m."-".$d;
    	
    	//check for time elements
    	if($matches[5][0]!="")
    	{
    		$dbDate.=" ".$matches[5][0];
    		$dbDate.=":".$matches[6][0];
    		if($matches[8][0]!="") 
    		{
    			$dbDate.=":".$matches[8][0];
    		}
    	}
    	return $dbDate;
    }
    function date_from_db($dateStr,$format="d/m/Y")
    {
    	$stamp=strtotime($dateStr);
    	if($stamp==-1) return false;
    	return date($format,$stamp);
    }
    I was basically wanting to check I wasn't re-inventing the wheel. It's been a fun journey and we got there in the end. Cheers

  14. #14
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by hessodreamy View Post
    But such usability talk is a bit off topic.
    You were right there, sorry.

  15. #15
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    >However what I'm working on right now is a back-office admin page so it means 1) I can rely a little more on the user

    Nah, it means you can punish them!

    >2) They're doing repetitive jobs so quick and easy is the way (ie not having a row of 3 selects).

    Of course you know your environment better than we can ever do, no wonder non-js talk was not of interest.

    >But such usability talk is a bit off topic.

    I don't agree, if you really want your app to work, this is the most important thing ... still, enuf dogma.

    All the same, if you have a good picture of how far in the future (or the past) that the majority of dates are being picked, then you could pre-select those input boxes, let say the month and the year - and leave the day as free-form.

    Using the same ideal - I hacked around the date boxes used in PhpMyAdmin and they work a treat, pick a date from a pop up box. Godsend.

    Date from db? You should read about the mysql format_date() function.

    Get the date out of your db in the format you want in the first place.

    Gd Luck

  16. #16
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    528
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Date from db? You should read about the mysql format_date() function.
    Normally I would, but in this case I'm going to need to order some dates AFTER the query, which is just easier to do in its native form YY-mm-dd.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •