How to convert string to date in php?

In this tutorial, we’ll explore several ways to convert String into Date objects.

1. Code:

    <?php 
	function converStringToDate($string){
		$date = substr($string, 0, 2);
		$month = substr($string, 2, 2);
		$year = substr($string, 4);
		return $year . "/" . $month . "/" . $date;
	}
		 
    $str = "19052022";	
	echo converStringToDate($str); 	
    ?> 

2. Example:

Input:

19052022

Output:

2022/05/19