I have strings like 20160101
and 20170204
, i want to divide them into array like
arr[0] = 2016;
arr[1] = 01;
arr[2] = 01;
Means first will be 4
character and other ones will be 2
and 2
.I can split it if there any character that can be used for explode.So need help in this issue.
arr[0] = substr("20160101", 0, 4);
arr[1] = substr("20160101", 4, 2);
arr[2] = substr("20160101", 6, 2);
Use this code
$orderdate = '20150102';
if (preg_match('#^(\d{4})(\d{2})(\d{2})$#', $orderdate, $matches)) {
$day = $matches[1];
$month = $matches[2];
$year = $matches[3];
}
What about doing something like this?
$date = "20160101";
$array['year'] = substr($date, 0, 4);
$array['month'] = substr($date, 4, 2);
$array['day'] = substr($date, 6, 2);
print_r($array);
The following should work, or at least point you in the right direction.
$arr[0] = substr($your_string, 0, 4);
$arr[1] = substr($your_string, 4, 2);
$arr[2] = substr($your_string, 6, 2);
For more information, see documentation of substr()