Search code examples
phpstringdatelaravel-5.3

How do you get the date from a dashed string to then use with laravels created_at?


So consider the following: 2017-04-09-some-text-here.md

the easiest way I can think of doing this is to split: explode("-", $string);

and then use the $result[0], $result[1], $result[2] to then some how create an appropriate Created At time stamp with Laravel (How I create that time stamp I have no idea)

So this is where I turn to stack, How do I properly and cleanly get the date from a piece of text as shown above, and then use it with laravel's created_at to set the created_at time stamp to 2017/04/09


Solution

  • $re = '/2\d{3}([- \/])\d{2}([- \/])\d{2}/';
    $str = 'TQ2 TR23RT2017-05-12 TR342T43T QT3 T34T34T2017/05/12 3tr3q2 tr R32T ';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    $dates = [];
    if(isset($matches)){
        foreach($matches as $match){
    
            $dates[] = str_replace('-','/',$match[0]);
        }
    }
    print_r($dates);