dongren4147 2017-10-05 03:27
浏览 35

碳 - 标准化日期时间

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?

  • 写回答

2条回答 默认 最新

  • douge3113 2017-10-05 03:34
    关注

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测