Split Time into 3 Variables

前端 未结 5 1845
鱼传尺愫
鱼传尺愫 2021-01-21 17:48

I have a time \"7:00 am\" that I need to split into $hour, $min, $ampm. Is there an easy way that I can split this in to 3 variables?

相关标签:
5条回答
  • 2021-01-21 17:53
    $yourtime = "7:00 am";
    preg_match("/([0-9]{1,2}):([0-9]{1,2}) ([a-zA-Z]+)/", $yourtime, $match);
    $hour = $match[1];
    $min = $match[2];
    $ampm = $match[3];
    
    0 讨论(0)
  • 2021-01-21 17:55
    $time = '7:00 am';
    list ($time, $ampm) = explode(' ', $time);
    list ($hour, $min) = explode(':', $time);
    
    0 讨论(0)
  • 2021-01-21 18:03
    sscanf( "7:00 am", "%d:%d %s", $hour, $min, $ampm );
    
    0 讨论(0)
  • 2021-01-21 18:03
    $time = '7:00 am';
    $hour = date("g",$time);
    $min = date("i",$time);
    $ampm = date("a",$time);
    

    http://php.net/manual/function.date.php

    0 讨论(0)
  • 2021-01-21 18:07
    $a      = preg_split('/[: ]/', '7:00 am');
    $hour   = $a[0][0];
    $minute = $a[1][0];
    $ampm   = $a[2][0];
    
    0 讨论(0)
提交回复
热议问题