SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If statement with two different strings?

    Hi All,

    Quick question. In php I'm dealing with a date, which has been parsed from a CSV file. And dates are in the following format:

    30/10/1897
    18/12/1894
    etc...

    Or

    Sat 19 Aug 00
    Tue 02 Sep 00
    etc...

    Basically I want to say If the date is in this one format do this, or if it's in the other format do this?

    How could I do this?

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    easiest way would be to ereg pattern match for the /

    PHP Code:
    if(ereg("/"$str)) {
    // do something

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Depending on how extensible you need it, future formats etc, you could use a little OOP*.

    PHP Code:
    #create parser
    $parser = new DateParser();

    #add new parser(s)
    $parser->addParser(new UKFormatDateParser());
    $parser->addParser(new OtherFormatParser());

    #notice how we don't tell it what parser to use
    echo date('r'$parser->parse('25/12/2009'));        #Fri, 25 Dec 2009 00:00:00 +0000
    echo date('r'$parser->parse('Tue 05 Sep 00'));    #Tue, 05 Sep 2000 00:00:00 +0000 
    Code:
    PHP Code:
    class DateParser
    {
        protected
            
    $parsers = array();
            
        public function 
    addParser(IParseDate $parser)
        {
            
    array_push($this->parsers$parser);
        }
        
        public function 
    parse($date)
        {
            foreach(
    $this->parsers as $parser)
            {
                if(
    $parser->isValid($date))
                {
                    return 
    $parser->getTimestamp();
                }
            }
            return 
    false;
        }
    }

    interface 
    IParseDate
    {
        public function 
    isValid($date);
        
        public function 
    getTimestamp();
    }

    class 
    UKFormatDateParser implements IParseDate
    {
        protected
            
    $timestamp null;
            
        public function 
    isValid($date)
        {
            if(
    false !== strtotime(str_replace('/''-'$date)))
            {
                
    $this->timestamp strtotime(str_replace('/''-'$date));
                return 
    true;
            }
            return 
    false;
        }
        
        public function 
    getTimestamp()
        {
            return 
    $this->timestamp;
        }
    }

    class 
    OtherFormatParser implements IParseDate
    {
        protected
            
    $timestamp null;
            
        public function 
    isValid($date)
        {
            if(
    false !== strtotime($date))
            {
                
    $this->timestamp strtotime($date);
                return 
    true;
            }
            return 
    false;
        }
        
        public function 
    getTimestamp()
        {
            return 
    $this->timestamp;
        }

    *disclaimer: It's early and I've not had a coffee yet.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guys

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    A slightly better example, although this does raise a question for me about handling exceptions which I may post on.

    I thought a caught exception didn't interrupt execution, in this example it breaks the foreach loop.

    PHP Code:
    $date_parser = new DateParserManager();
    $date_parser->addParser(new UKDateParser());
    $date_parser->addParser(new DefaultDateParser());
    try
    {
        foreach(array(
    '25/12/2009''my sisters birthday''this tuesday''tomorrow''yesterday''1 day after my birthday') as $date)
        {
            echo 
    date('r'$date_parser->parse($date)), '<br />';
        }
    }
    catch(
    Exception $exception)
    {
        echo 
    $exception->getMessage(), '<br />';

    PHP Code:
    class DateParserManager
    {
        protected
            
    $parsers = array();
        
        public function 
    parse($date)
        {
            foreach(
    $this->parsers as $parser)
            {
                if(
    $parser->isValid($date))
                {
                    return 
    $parser->getTimestamp();
                }
            }
            throw new 
    Exception(sprintf('%s cannot be parsed into a UNIX timestamp'$date));
        }
        
        public function 
    addParser(DateParser $parser)
        {
            
    array_push($this->parsers$parser);
        }
    }

    abstract class 
    DateParser
    {
        protected
            
    $timestamp null;
        
        public function 
    getTimestamp()
        {
            return 
    $this->timestamp;
        }
        
        abstract public function 
    isValid($date);
    }

    class 
    UKDateParser extends DateParser
    {
        public function 
    isValid($date)
        {
            
    $date strtotime(
                
    str_replace(
                    
    '/',
                    
    '-',
                    
    $date
                
    )
            );
            if(
    true === is_int($date))
            {
                
    $this->timestamp $date;
                return 
    true;
            }
            return 
    false;
        }
    }

    class 
    DefaultDateParser extends DateParser
    {
        public function 
    isValid($date)
        {
            
    $date strtotime($date);
            
            if(
    true === is_int($date))
            {
                
    $this->timestamp $date;
                return 
    true;
            }
            return 
    false;
        }

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    easiest way would be to ereg pattern match for the /

    PHP Code:
    if(ereg("/"$str)) {
    // do something

    My advice would be to not use ereg anymore.. as of PHP 5.3, it (POSIX) is considered depreciated, and will not be included within the core of PHP as of version 6. Learn PCRE instead.

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    fair point nrg preg_match instead then
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    easiest way would be to ereg pattern match for the /
    Regexes are for matching patterns, not simple character lookup.

    PHP Code:
    if (strpos($str"/") !== FALSE) {
        
    // do something


  9. #9
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    //$str = '30/10/1897';
    $str 'Sat 19 Aug 00';

    function 
    one($d$m$y) {
        echo 
    "$y$m$d";
    }
    function 
    two($wd$d$m$y)
    {
        
    $mmap array_combine(array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'), range(1,12));
        
    one(substr("0$d", -2), substr("0{$mmap[strtolower($m)]}", -2), "20$y");
    }

    switch(
    true)
    {
        case (bool)
    preg_match('#^\s*(\d{1,2})\s*/\s*(\d{1,2})\s*/\s*(\d{4})\s*$#'$str$match):
            
    one($match[1], $match[2], $match[3]);
        break;
        
        case (bool)
    preg_match('#^\s*(sun|mon|tue|wed|thu|fri|sat)\s+(\d{1,2})\s+(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\s+(\d{2})*\s*$#i'$str$match):
            
    two($match[1], $match[2], $match[3], $match[4]);
        break;
        
        default:
            echo 
    "None: $str";
    }

    ?>

  10. #10
    SitePoint Enthusiast dakine's Avatar
    Join Date
    Dec 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    keep in mind that you could ignore all of these and just pass the date in either format into strtotime() and then have a unix timestamp with which you can format however you want using date()

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dakine View Post
    keep in mind that you could ignore all of these and just pass the date in either format into strtotime() and then have a unix timestamp with which you can format however you want using date()
    Or not.


    The first format supplied by the OP is not strtotime() compatible.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


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
  •