duanjiao6711 2015-05-26 07:43
浏览 48
已采纳

如何在PHP中拆分不同的日期格式?

I have some dates like this :

  • 15 Décembre 2012
  • Du mardi 18 avril au jeudi 25 mai
  • Le 25, 26 et 27 Juin

Just examples in French.

My database looks like :

 CREATE TABLE DateTest(id_date int(11), date text, date_deb date, date_fin date);

I would like to insert my different dates regardless format of them. I can easily insert the first example, the second too, but concerning the first, I don't know..

It can have few separators (, or et), and when I get the number of the day (25 for the first), I don't have the month.

  • 写回答

1条回答 默认 最新

  • donglv6747 2015-05-26 08:10
    关注

    What I would do, is write several regular expressions for catching the different ways that these date periods can be entered. For the third format, for example:

    $month_nrs = array('janvier' => 1, 'février' => 2, 'mars' => 3, 'avril' => 4, 'mai' => 5, 'juin' => 6, 'juillet' => 7, 'août' => 8, 'septembre' => 9, 'octobre' => 10, 'novembre' => 11, 'décembre' => 12);
    
    $period = 'Le 25, 26 et 27 Juin';
    //$period = 'Le 25 et 26 Juin';
    //$period = 'Le 25, 26, 27 et 28 Juin';
    if (preg_match('/(\d{1,2})(?:,\s*\d{1,2})* et (\d{1,2})\s+(janvier|février|mars|avril|mai|juin|juillet|août|septembre|octobre|novembre|décembre)/i', $period, $matches)) {
        $month_name = strtolower($matches[3]);
        $month_nr = $month_nrs[$month_name];
        $day_deb = $matches[1];
        $day_fin = $matches[2];
        $date_deb = date('Y').'-'.$month_nr.'-'.$day_deb;
        $date_fin = date('Y').'-'.$month_nr.'-'.$day_fin;
    
        echo "Date deb: $date_deb, date fin: $date_fin";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算