碳 - 标准化日期时间

I am working on some data migration process, with consists of loads of CSV's

The problem I'm facing now is that the data file I am working with has 2 columns, namely "date" and "time". Now, date itself doesn't have a problem. My "time" column can come in any format, i.e. H:i, or H:i:s. Regardless of the format, I want my output to be d/m/Y H:i:s, and to just append 00 if seconds is not given.

Carbon's createFromFormat requires me to specify what my input format is, which I can't. Currently what I am doing is using Excel to split my CSV files into different files according to the time format, then import them separately, but it's taking too much time.

I can't seem to find a function that allows me to do this either:

if(getSecondsFromTime){
  Carbon::createFromFormat($string, 'd/m/Y H:i:s');
}else{
  Carbon::createFromFormat($string, 'd/m/Y H:i');
}

Does Carbon/PHP Datetime has a function that I am unaware of that can do this? Or do I have to resolve to regex to do what I need?

As you process each row in Excel, get your date and time values. Then use a regular expression to check the time format. Use that to dictate what format to use when creating your Carbon instance.

$date = '05/10/2017';
$time = '13:31:00'; // or '13:31'

$format = preg_match('/^\d{2}:\d{2}$/', $time) ? 'd/m/Y H:i' : 'd/m/Y H:i:s');
$datetime = Carbon::createFromFormat("{$date} {$time}", $format);

If you replace your / with - then Carbon::parse will work:

$t = '05/10/2017 18:00';
$c = Carbon::parse(str_replace('/', '-', $t));