dongliao1860
2010-07-27 22:00
浏览 65
已采纳

将时间字符串转换为UNIX时间戳

How can I convert a time string like this:

30/7/2010

to a UNIX timestamp?

I tried strtotime() but I get a empty string :(

图片转代码服务由CSDN问答提供 功能建议

如何转换时间字符串,如下所示:

< p> 30/7/2010

到UNIX时间戳?

我试过 strtotime()但是我得到一个空字符串:(

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duandu2159 2010-07-27 22:19
    已采纳

    PHP >= 5.3:

    $var = DateTime::createFromFormat('j/n/Y','30/7/2010')->getTimestamp();
    
    打赏 评论
  • dongya0914 2010-07-27 22:04

    You probably want to use http://us3.php.net/manual/en/function.strptime.php (strptime) since your date is in a different format than what PHP might be expecting.

    打赏 评论
  • dongpa2000 2010-07-27 22:05

    You could also convert it to a format that strtotime() can accept, like Y/M/D:

    $tmp = explode($date_str);
    $converted = implode("/", $tmp[2], $tmp[1], $tmp[0]);
    $timestamp = strtotime($converted);
    
    打赏 评论
  • duanlanqian9974 2010-07-27 22:07

    You're using UK date format.

    Quick and dirty method:

    $dateValues = explode('/','30/7/2010');
    $date = mktime(0,0,0,$dateValues[1],$dateValues[0],$dateValues[2]);
    
    打赏 评论
  • duandu5846 2010-07-27 23:12

    The PHP 5.3 answer was great

    DateTime::createFromFormat('j/n/Y','30/7/2010')->getTimestamp();
    

    Here is a < 5.3.0 solution

    $timestamp = getUKTimestamp('30/7/2010');
    
    function getUKTimestamp($sDate) {
        list($day, $month, $year) = explode('/', $sDate);
        return strtotime("$month/$day/$year");
    }
    
    打赏 评论

相关推荐 更多相似问题