19

I have a date in this format: 20101101120000

I need to convert it to a timestamp with PHP.

I've been searching the PHP docs online, but can't find anything that can convert directly. Does one exist? If not, what's the most efficient way to do the conversion? Thank you for your help.

1
  • 6
    What would the expected output be for this example?
    – Gumbo
    Commented Jan 30, 2011 at 14:35

4 Answers 4

26

You can do this with DateTime::createFromFormat:

$d = DateTime::createFromFormat('YmdGis', '20101101120000');

$d is now a DateTime instance. You can either convert it to a timestamp with $d->getTimestamp() or use the DateTime methods on it.

Note that this requires PHP 5.3.

1
  • 1
    I'd just worked out it was YmdGis when your answer popped up... +1 for the fast response
    – Mark Baker
    Commented Jan 30, 2011 at 14:40
6
strtotime('20101101120000')

....

4
  • 1
    Have you tried it? Does it work? From the docs: The function expects to be given a string containing an English date format Commented Jan 30, 2011 at 14:53
  • This does work and is most useful when working with MySQL. Also note that it will do the conversion to GMT basing on currently set timezone.
    – Mchl
    Commented Jan 30, 2011 at 14:55
  • Interesting... I thought it would be more restrictive regarding this formats but... cool :) +1 Commented Jan 30, 2011 at 14:57
  • @Felix King: when in doubt check here: php.net/manual/en/datetime.formats.php
    – Mchl
    Commented Jan 30, 2011 at 14:58
2

You need the function strptime.

The formats are described at strftime.

2
  • +1 - I don't see why this doesn't have more upvotes! Works back to PHP 4, too.
    – uınbɐɥs
    Commented Aug 26, 2012 at 6:19
  • Note: Internally, this function calls the strptime() function provided by the system's C library. This function can exhibit noticeably different behaviour across different operating systems. The use of date_parse_from_format(), which does not suffer from these issues, is recommended on PHP 5.3.0 and later.
    – mariusnn
    Commented Sep 23, 2013 at 10:48
2

I only add to this resolved question because this may be helpful for users who stumble upon here (like I did) and are looking to get an actual datetime timestamp.

My assumption for most folks when they mention timestamp is that they're looking for a normal "YYYY-MM-DD HH:MM:SS" timestamp not a unix timestamp which you get when you use the getTimestamp() method (see accepted answer if you are indeed looking for the UNIX Timestamp).

So I will further elaborate on lonesomeday's answer (which is fully correct) for those looking to actually get a valid formated date that they can display to users or insert into their database:

$d = DateTime::createFromFormat('YmdGis', '20101101120000');

$formatedDate = $d->format('Y-m-d H:i:s'); // will return 2010-11-01 12:00:00
$formatedDate = $d->format('m-d-Y h:i A'); // will return 11-01-2010 12:00 PM 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.