Sourabh
Sourabh

Reputation: 1765

Converting timestamps in php

I got time stamp in this format from twitter api.

Fri Dec 28 20:06:38 +0000 2012

I want to convert this to standard time stamp format like this one.

2012-12-10 16:20:18

Am pretty new to dates in php. How can I do it??

Upvotes: 0

Views: 81

Answers (4)

Leri
Leri

Reputation: 12535

You can use DateTime:

$date = new DateTime('Fri Dec 28 20:06:38 +0000 2012');
echo $date->format('Y-m-d H:i:s');

The reason why I prefer DateTime is that it gives great oop implementation and makes it easier to work with dates that are quite big head-ache of programmer. For more information about this class read the manual that I have already referenced.

Upvotes: 5

Igor Parra
Igor Parra

Reputation: 10348

In case of use php < 5.2.0 (some shared hostings) use a combination of strtotime and date

date('Y-m-d H:i:s', strtotime('Fri Dec 28 20:06:38 +0000 2012'));

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use this

 $originalDate = "Fri Dec 28 20:06:38 +0000 2012";
 echo $newDate = date("Y-m-d h:i:s", strtotime($originalDate));

working example http://codepad.viper-7.com/AhLcEU

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318468

You can use strtotime to convert the initial string to a UNIX timestamp and then strftime to convert it back to a string:

strftime('%Y-%m-%d %H:%M:%S', strtotime('Fri Dec 28 20:06:38 +0000 2012'));

That call returns the string 2012-12-28 21:06:38 - i.e. exactly what you are looking for.

Upvotes: 1

Related Questions