Skip to content
Wade Womersley

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:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on LinkedIn (Opens in new window) LinkedIn
  • 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

AI
Gemini Tooling Updates Show Agents Are About Orchestration
May 5, 2026
Programming
Hidden things, lack of fries and pointless data
September 4, 2009
Programming
Using Amazon EC2 with C# .net
October 15, 2012
Programming
Reset MySQL root password if you forgot it #mysql
April 12, 2010

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 © 2026 wade.one.

Theme: Oceanly News Dark by ScriptsTown