I have lots of date and time data which have been put together like so 05/12/2113:30
What I want to do is separate into two strings like so $date = '05/12/21'
and $time = 13:30
so that I can prepare them for database entry in a correct format.
They are always the same first 8 digits (including 2 '/') are the date and the last 5 digits (including ':') are the time.
How can i go about separating them correctly using php?
Thanks so much for your help and I am sure its easy I just seem to be having a brain fart moment.
CodePudding user response:
$value = "05/12/2113:30";
var_dump(substr($value, 0, 8)); //05/12/21
var_dump(substr($value, 8, 5)); //13:30
CodePudding user response:
Using substr() you can extract from any place.
$string = '05/12/2113:30';
$date = substr($string, 0, 8); // 05/12/21 <-- from start
$time = substr($string, -5); // 13:30 <-- from end
CodePudding user response:
You can use substr:
- first substring will be date (from 0 to 8)
- second substring will be time (from 8 to the end of string)
$date = substr($str,0,8);
$time = substr($str,8);
CodePudding user response:
Using the builtin DateTime Object its actually quite easy when you can guarantee the input format.
$in = '05/12/2113:30';
$dt = (new DateTime())->CreateFromFormat('d/m/yG:i', $in);
echo 'database format = ' . $dt->format('Y-m-d H:i:s');
RESULT
datebase format = 2021-12-05 13:30:00