Lebih
How to convert date to timestamp in PHP?
How do I get timestamp from e.g. 22-09-2008
?
334
19
How do I get timestamp from e.g. 22-09-2008
?
PHP's
strtotime()
givesWhich does work with the Supported Date and Time Formats Docs.
There is also
strptime()
which expects exactly one format:With
DateTime
API:The same with the procedural API:
If the above fails because you are using a unsupported format, you can use
Note that if you do not set the
!
, the time portion will be set to current time, which is different from the first four which will use midnight when you omit the time.Yet another alternative is to use the
IntlDateFormatter
API:Unless you are working with localized date strings, the easier choice is likely DateTime.
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.If you have PHP 5.3 or above,
this method works on both Windows and Unix and is time-zone aware, which is probably what you want if you are serious about working with dates.
If you don't care about timezone, or want to use the time zone your server uses:
1222093324 (This will differ depending on your server time zone...)
If you want to specify in which time zone, here EST. (Same as New York.)
1222093305
Or if you want to use UTC. (Same as "GMT".)
1222093289
Using mktime:
Using strtotime() function you can easily convert date to timestamp
More info: http://php.net/manual/en/function.strtotime.php
Online conversion tool: http://freeonlinetools24.com/
Here is a very simple and effective solution using the
split
andmtime
functions:It worked like a charm for me.
Given that the function
strptime()
does not work for Windows andstrtotime()
can return unexpected results, I recommend usingdate_parse_from_format()
: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 know the format use
strptime
becausestrtotime
does a guess for the format, which might not always be correct. Sincestrptime
is not implemented in Windows there is a custom functionRemember that the returnvalue
tm_year
is from 1900! andtm_month
is 0-11Example:
Use PHP function
strtotime()
date — Format a local time/date
If you want, put it in a MySQL input type timestamp. The above works very well (only in PHP 5 or later):
Here is how I'd do it:
This way, you let the function know what date format you are using and even specify the timezone.
Please be careful about time/zone if you set it to save dates in database, as I got an issue when I compared dates from mysql that converted to
timestamp
usingstrtotime
. you must use exactly same time/zone before converting date to timestamp otherwise, strtotime() will use default server timezone.Please see this example: https://3v4l.org/BRlmV
If you're looking to convert a UTC datetime (
2016-02-14T12:24:48.321Z
) to timestamp, here's how you'd do it: