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

Programming
C# MySqlDataReader – “one line” function to return a row as an instance of a class.
September 18, 2013
PHP
PHP 8.2 vs 7.4
March 26, 2023
PHP
PHP 8.2 Needs an Upgrade Plan Before the End of 2026
October 13, 2026
AI
Codex Is Moving Into the Whole Development Workflow
May 14, 2026

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

Categories

  • 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

Archives

  • November 2026
  • October 2026
  • August 2026
  • July 2026
  • June 2026
  • May 2026
  • April 2026
  • March 2026
  • August 2023
  • April 2023
  • March 2023
  • April 2016
  • October 2015
  • July 2015
  • June 2015
  • May 2015
  • March 2015
  • February 2015
  • January 2015
  • October 2014
  • December 2013
  • September 2013
  • June 2013
  • October 2012
  • May 2012
  • March 2012
  • February 2012
  • April 2011
  • March 2011
  • October 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009

Copyright © 2026 wade.one.

Theme: Oceanly News Dark by ScriptsTown