Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

murilo's avatar
Level 10

extract date and time from a string with preg_match_all

hello , I want to extract a date ( 10/10/2021 12:20 ) with time from a text , like this - Text -

Lorem Ipsum is simply dummy  10/10/2021  12:20  text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown 

I have this code in php , that extract only the date -

 if(preg_match_all('/\d{2}\/\d{2}\/\d{4}/',$string,$matches)){
           return $matches;
        }
        return null;

The result -

10/10/2021

How could I extract the time as well ? I have tried many , options . Thanks

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

/\d{2}\/\d{2}\/\d{4}\s+\d{2}:\d{2}/

Please or to participate in this conversation.