preg_split
Separate strings by a regular expression
preg_split()
function uses the match of the regular expression as a delimiter to split the string into an array.
Use preg_split() to split the date into its components:
<?php $date = "1970-01-01 00:00:00" ; $pattern = "/[-\s:]/" ; $components = preg_split ( $pattern , $date ) ; print_r ( $components ) ; ?>
Try it yourself
Use the PREG_SPLIT_DELIM_CAPTURE flag:
<?php $date = "1970-01-01 00:00:00" ; $pattern = "/([-\s:])/" ; $components = preg_split ( $pattern , $date , - 1 , PREG_SPLIT_DELIM_CAPTURE ) ; print_r ( $components ) ; ?>
Try it yourself
Use the PREG_SPLIT_OFFSET_CAPTURE flag:
<?php $date = "1970-01-01" ; $pattern = "/-/" ; $components = preg_split ( $pattern , $date , - 1 , PREG_SPLIT_OFFSET_CAPTURE ) ; print_r ( $components ) ; ?>
Try it yourself