0

I want to change this date format: "Tue Apr 3 15:00:03 GMT+0300 2012" to "3.4.2012" with PHP. Is it possible?

I tried:

$date="Tue Apr 3 15:00:03 GMT+0300 2012";
echo date('d.m.Y', strtotime($date));

but it results in: 03.04.2015. What am I doing wrong?

TRiG
  • 10,148
  • 7
  • 57
  • 107
sstauross
  • 2,602
  • 2
  • 30
  • 50
  • 1
    possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – PeeHaa Apr 03 '12 at 12:54
  • I've run this on PHP 5.3.18 (Fedora 14) and it gives the correct results, what are you using? – h00ligan Apr 03 '12 at 13:10

2 Answers2

4

Read the Manual before using a function, and especially before asking other people to read it for you, like I just did to answer your question:

date('j.n.Y', strtotime($date));

Codepad Example

Paul
  • 139,544
  • 27
  • 275
  • 264
  • i think his problem is the incorrect year and not the leading zeroes – scibuff Apr 03 '12 at 12:51
  • I think that the problem in his case, is that strtotime() is not parsing the date string properly and not the leading zeros of day/month. – mobius Apr 03 '12 at 12:52
  • 1
    @scibuff Pretty sure it's the leading zeros. strtotime has no trouble parsing that date: http://codepad.viper-7.com/K3s1Pv I think the 2015 in the OP is just a typo – Paul Apr 03 '12 at 12:53
  • @mobius See response to scibuff – Paul Apr 03 '12 at 12:53
  • @PaulP.R.O it does not work for me and the possible fact is that it does not accept the format. You could not answer if you didnt want to. And just to know, there are no silly questions just silly answers... – sstauross Apr 05 '12 at 12:28
3

Your date is not formatted correctly, at least it is not the RFC 2822 date format,

Tue, 3 Apr 2012 15:00:03 +0300

strtotime is quite flexible but it cannot guess what you mean ...

scibuff
  • 13,377
  • 2
  • 27
  • 30
  • strtotime can take in tons of Date/Time formats. It doesn't have to be RFC 2822: http://ca2.php.net/manual/en/datetime.formats.php – Paul Apr 03 '12 at 12:55
  • yes, but it looks like that RFC 2822 is the format he's trying to use – scibuff Apr 03 '12 at 13:27
  • its an actionscript format that i want to transfer in a web page..What i finally did is to split it and give it into date() as one of the formats accepted by php so the date() now knows how to convert it. – sstauross Apr 05 '12 at 12:31