E
E
elenapetrova19952018-03-23 13:01:03
PHP
elenapetrova1995, 2018-03-23 13:01:03

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'

What is the best way to parse such data as a date?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlxMrz, 2018-03-24
@AlxMrz

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);

Could be so:
$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);

M
Maxim Timofeev, 2018-03-23
@webinar

https://habrahabr.ru/post/123845/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question