dousi6405 2017-10-03 04:40
浏览 60
已采纳

输出日期与原始日期不同

Here's my problem. output date different from the string i cut. I've tried the code below and the day not match with the original date. it should "26" but output "02", give me solution.

CODE :

$line = 'File=D:\Shared\print\2017\september\26\printing\baryun - cetak transferpaper.tif';
     $r = "/^File=D:\\\\[a-zA-Z]+\\\\[a-z]+\\\\([0-9]{4})\\\\([a-z]+)\\\\([0-9]{1,2})/";

       if (preg_match($r, $line, $match)) {
            $date = $match[1]."-".$match[2]."-".$match[3];
            echo $date."<br>";
            $xtime = strtotime($date);
            $tes = date("Y-m-d",$xtime);

            echo $tes;
          }

OUTPUT :

2017-september-26
2017-09-02

And when i try this code

 if (preg_match($r, $line, $match)) {
            $date = $match[1]."-".$match[2]."-".$match[3];
            echo $date."<br>";
            $xtime = date_create($date);
            $tes = date_format($xtime,"Y-m-d");

            echo $tes;

OUTPUT, the day 01. please help me

2017-september-26
2017-09-01
  • 写回答

2条回答 默认 最新

  • douhuang4166 2017-10-03 04:49
    关注

    It's because the date string you are trying to pass is not-correctly formatted.

    Do like below:-

    $r = "/([0-9]{4})\\\\([a-z]+)\\\\([0-9]{1,2})/"; // curtale your regex
    if (preg_match($r, $line, $match)) {
        $date = $match[3]."-".$match[2]."-".$match[1]; //change the pattern
        echo $date."<br>";
        $xtime = strtotime($date);
        $tes = date("Y-m-d",$xtime);
    
        echo $tes;
    }
    

    https://eval.in/872478

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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