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

Gaming
Epic Games releases Unreal Development Toolkit for FREE
November 7, 2009
AI
Codex Is Moving Into the Whole Development Workflow
May 14, 2026
AI
Gemini Tooling Updates Show Agents Are About Orchestration
May 5, 2026
PHP
PHP 8.2 vs 7.4
March 26, 2023

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