简体   繁体   English

PHP日期/时间到时间戳(自定义格式)

[英]PHP date/time to timestamp (custom format)

Any one got a function to like strtotime() but the takes the format as input? 任何人都有一个喜欢strtotime()的函数,但是将格式作为输入?

For example I need to convert yyyymmdd to a timestamp, or perhaps yyyyddmm . 例如,我需要将yyyymmdd转换为时间戳,或者yyyyddmm So I would like to specify the format used. 所以我想指定使用的格式。 Also Im on Windows so strptime() isn't an option. 我也在Windows上,因此strptime()不是一个选项。

PHP 5's DateTime class has the createFromFormat method which does what you need. PHP 5的DateTime类具有createFromFormat方法, createFromFormat您的需要。

However, it requires PHP 5.3 so it's not always an option (yet). 但是,它需要PHP 5.3,因此它并不总是一个选项。

Very easy to write a function, just take a look at mktime ... 很容易写一个函数,只需看看mktime ...

// Assumes yyyy-mm-dd
function fromdate($date) {
    $d = explode("-", $date);
    return mktime(0, 0, 0, $d[1], $d[2], $d[0]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM