duanjiao6711 2015-05-25 23: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 00: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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部