Skip to content
wade.one

wade.one

wade womersley – york based software engineer

  • Home
  • 2012
  • February
  • 5
  • Regular Expression (Regex) For Date part extraction/array split

Regular Expression (Regex) For Date part extraction/array split

Posted on February 5, 2012 By Wade No Comments on Regular Expression (Regex) For Date part extraction/array split
Programming

Needed to use this recently to create a specificity parameter for an API at work, it gives you a key/value array from an ISO date string regardless of what is actually set in the string (e.g. if you give 2011-06-07 04 you’ll get an array back as array(‘year’=>2011, ‘month’ => 6, ‘day’ => 7, ‘hour’ => 4). If you enter 2011-06-07 04:43 you’ll get: array(‘year’=>2011, ‘month’ => 6, ‘day’ => 7, ‘hour’ => 4, ‘minute’ => 43), works from just a year all the way to seconds.

    /**
     * Gets the parts of a date that are set in an ISO date string.
     * 
     * @param string $date date/time to use.
     * 
     * @return array
     */
    public static function getDateParts($date)
    {
        $matches = array();
        preg_match(
            '/(
                (
                    (
                        (
                            (
                                (?<year>d{4})
                                (-(?<month>d{2}))?
                            )
                            (-(?<day>d{2}))?
                        )
                        (s?(?<hour>d{2}))?
                    )
                    (:(?<minute>d{2}))?
                )
                (:(?<second>d{2}))?
            )/x', 
            $date, 
            $matches
        );

        $date = array_map('intval', array_intersect_key($matches, array_flip(array('year', 'month', 'day', 'hour', 'minute', 'second'))));

        return $date;
    }

Share:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Reddit (Opens in new window) Reddit

Related

Comments

comments

Post navigation

❮ Previous Post: Automating “Implemented methods” for a web based API
Next Post: PHP 5.4 Unofficial Ubuntu apt-get available ❯

You may also like

Programming
Cloudant’s Rather Awesome dbcopy for re-reducing the output of existing map/reduce data.
June 23, 2013
Programming
#leedsphp talk slides uploaded
April 22, 2011
Ego-centric
Today’s musings
September 3, 2009
Programming
phantomjs custom module – require and create
December 9, 2013

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • AI
  • artificial intelligence
  • Ego-centric
  • Events
  • Films
  • Food
  • Gaming
  • Gym
  • Hardware
  • Holidays
  • News
  • PHP
  • Programming
  • Random Stuff
  • Reviews
  • Science
  • SEO
  • Software
  • Software Engineer
  • Support
  • Uncategorized
  • Work

Copyright © 2025 wade.one.

Theme: Oceanly News Dark by ScriptsTown