How do I get timestamp from e.g. 22-09-2008
?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Using mktime:
If you want to know for sure whether a date gets parsed into something you expect, you can use
DateTime::createFromFormat()
:It's obvious in this case, but e.g.
03-04-2008
could be 3rd of April or 4th of March depending on where you come from :)http://php.net/manual/en/function.date.php
If you're looking to convert a UTC datetime (
2016-02-14T12:24:48.321Z
) to timestamp, here's how you'd do it:Be careful with functions like
strtotime()
that try to "guess" what you mean (it doesn't guess of course, the rules are here).Indeed
22-09-2008
will be parsed as 22 September 2008, as it is the only reasonable thing.How will
08-09-2008
be parsed? Probably 09 August 2008.What about
2008-09-50
? Some versions of PHP parse this as 20 October 2008.So, if you are sure your input is in
DD-MM-YYYY
format, it's better to use the solution offered by @Armin Ronacher.PHP's
strtotime()
givesWhich does work with the Supported Date and Time Formats Docs.