Answer the question
In order to leave comments, you need to log in
How to save time?
What is the best way to parse such a time format?
There are a lot of records like
'8:00a'
'8:00p'
'2:00p'
'12:50p'
Answer the question
In order to leave comments, you need to log in
It is possible like this:
$timeList = [
'8:00a',
'8:00p',
'2:00p',
'12:50p'
];
$parsedTime = [];
foreach($timeList as $date) {
$time = explode(":",$date);
$hours = $time[0];
$minutes = substr($time[1], 0, mb_strlen($time[1])-1);
$timeType=substr($time[1], -1);
$parsedTime[] = [
'hours' => $hours,
'minutes' => $minutes,
'type' => $timeType
];
}
var_dump($parsedTime);
$timeList = [
'8:00a',
'8:00p',
'2:00p',
'12:50p'
];
$parsedTime = [];
foreach($timeList as $date) {
preg_match('/(\d{1,2}):(\d{2})(a|p{1})/', $date, $matches);
$parsedTime[] = [
'hours' => $matches[1],
'minutes' => $matches[2],
'type' => $matches[3]
];
}
var_dump($parsedTime);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question