0

How to change date format dmYhis to Y-m-d in PHP. I have using the date() function but I cannot seem to make it work

   $d = '24032017191551';

   echo date('Y-m-d', strtotime($d));

Output: 1970-01-01

i want to get the ans is 2017-03-24

  • This is not correct duplicate! He is going above the 32 bit limit of strtotime(). See here https://3v4l.org/UHWTB – Andreas May 26 '17 at 05:08
  • @HankyPanky ? Reopen and let me answer the question. None of the answers on the page you link to is correct for this question – Andreas May 26 '17 at 05:17
  • That suggestion is not the correct answer. There is no 32 bit limit in action here. You're assuming that they are proving a Unix timestamp, they are not. Its an improperly formatted datetime. '24032017191551' => `24-03-2017 19:15:51`. That Fiddle is misguided. – Hanky Panky May 26 '17 at 06:18
  • @Hanky i see that now. Thanks – Andreas May 26 '17 at 10:11

1 Answers1

2

Below is the code to change date format

      $retrieved ='24032017191551';
      $date = DateTime::createFromFormat('dmYHis', $retrieved);
      echo $date->format('Y-m-d');
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Pradeep
  • 267
  • 4
  • 15
  • $date=date_create("24032017191551"); echo date_format($date,"Y/m/d");i got thi s error :date_format() expects parameter 1 to be DateTimeInterface, boolean given on line 2 – Aparna P M May 26 '17 at 05:03
  • @AparnaPM..can u tell me the format of the date u are entering.. – Pradeep May 26 '17 at 05:18
  • date('dmYhis') format – Aparna P M May 26 '17 at 05:26
  • Your answer is not correct, you just changed the input to fit the date he wants. I usually don't downvote but this is really bad. – Andreas May 26 '17 at 05:56
  • @Andreas...she has not mentioned that she requires hour as well as minutes....then its not a problem... – Pradeep May 26 '17 at 05:59
  • @Andreas..if u have better suggestion then please post it'll improve my programming skill also... – Pradeep May 26 '17 at 06:01
  • @Pradeep I'm not talking about the output. I'm talking about you substr(..., 0, 8). You take the value and just delete the last 6 digits to make it correct. I have written what is the issue with this question above in comments but since HankyPanky closed the question I can not post an answer to the question. – Andreas May 26 '17 at 06:03
  • 1
    @Andreas..I hope now the answer is ok..because ive tested it and got desired output... – Pradeep May 26 '17 at 06:08
  • This answer, as it currently stands, is the correct answer for the (duplicate) question being asked. – Hanky Panky May 26 '17 at 06:22
  • @AparnaPM...since uve not accepted my answer...i hope my answer was helpful in clearing the error.. – Pradeep May 26 '17 at 07:02