0

I'm writing some code a script, which has short dates like

14/12/17
30/11/17
20/11/17

I need to convert these to long date format so i used PHP date function and strtotime as below

echo date('d-m-Y',strtotime('14/12/17'));

But it always getting 01-01-1970 as output, but it should be 14-12-2017

anyone know how to convert this to a long date format please.

PS. Other question answers suggest change the date input format, but I cannot change date input since it's getting from another site

Suneth Kalhara
  • 1,116
  • 4
  • 16
  • 40
  • 1
    Why don't you just replace the `/` with `-`? Sorry I see now that you want 2017, not only 17. – Andreas Dec 30 '17 at 07:00
  • whatever you do output will be **0017-12-14**. ***Wrong format on date saving***. Because system never know 17 means `1917 ` or `2017` or `2117` – Abdulla Nilam Dec 30 '17 at 07:11
  • @Andreas no that question answer he generate short date using php date function, in my question i just use predefined short dates – Suneth Kalhara Dec 30 '17 at 07:14
  • @AbdullaNilam I cannot changed date input format, becoz which comes from another site, is there have solution – Suneth Kalhara Dec 30 '17 at 07:16
  • 1
    no way. as simple wrong date format. if I send 16 how could you tell its belongs to 2016, and why not 1916 ?? got the point ? – Abdulla Nilam Dec 30 '17 at 07:17
  • 1
    can give solution to fix this **01-01-1970 as output, but it should be 14-12-2017** but output will be `14-12-0017` not `14-12-2017` – Abdulla Nilam Dec 30 '17 at 07:18
  • Thanks, I'll try some other solution – Suneth Kalhara Dec 30 '17 at 07:24
  • @Suneth no read the full thread and answers and you will see. As an example, this fits your needs perfectly. https://stackoverflow.com/a/11435513/5159168 – Andreas Dec 30 '17 at 07:32

2 Answers2

1

This is from the link I posted and OP says is not correct.
Originally posted by ceiroa.
Convert one date format into another in PHP

$myDateTime = DateTime::createFromFormat('d/m/y','14/12/17');
$newDateString = $myDateTime->format('d-m-Y');

Echo $newDateString;

https://3v4l.org/2AcAd

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • If you know this is a duplicate question you should vote to close as such, not repost a duplicate answer from it. – John Conde Dec 30 '17 at 14:52
  • @JohnConde I did see the closevote on the question above? But since neither OP or anyone else believed it's a duplicate I had to make it an answer and prove it. You can remove your downvote to, that is not what they are ment to be used for. – Andreas Dec 30 '17 at 15:16
  • You should never answer questions you vote to close as it allows you to get rep and prevent others from doing so after it is closed. You also shouldn't answer question to prove anything to anyone. You can do that in a comment if you really feel you need to prove something to someone. But is it *that* important to prove something to someone at Stack Overflow? If they're ignorant just move on. – John Conde Dec 30 '17 at 15:22
  • @JohnConde this thread has 41 views, I posted the duplicate only a few minutes after question was posted if I remember correctly. That means 30+ viewers are ignorant in your opinion that they didn't see the duplicate and casted their votes? It's not about it being important, I just feel the answers that came here (the two I posted comments too) was not great. In my opinion it was obvious OP needed more help than first replace then explode and rearrange and use date to rebuild again. It feelt so backwards and since I did not get any help in the closing of the thread it would be better to at... – Andreas Dec 30 '17 at 15:33
  • Least post a proper answer before the thread dies away. What are people going to think if they found this thread with the "explode" answers? – Andreas Dec 30 '17 at 15:34
  • If an answer is bad downvote it. That's how users tell other users an answer is bad. Posting another answer when the whole question should go away is *not* the right way to handle it. Additionally, pointing the OP to the canonical question should have been enough to help them but if you wanted to do more you could have left a comment. But by posting an answer, especially after voting to close which is a big no-no, you perpetuate the problem of bad questions being asked at Stack Overflow. – John Conde Dec 30 '17 at 15:37
  • This thread really should just be deleted as it serves to benefit to anyone. – John Conde Dec 30 '17 at 15:38
  • @JohnConde dude?! Read the comments above! I have voted and I have told OP that it is a duplicate, I even posted a link to the actual answer he/she needs to read. What more do you think should be done? Also posting an closevoting is very common in PHP tag. Browse around a little and you will see. – Andreas Dec 30 '17 at 15:40
  • That's good that you downvoted. But doing so doesn't mean you should answer a bad question. Just downvotes, vote to close, and *walk away*. – John Conde Dec 30 '17 at 15:43
  • Thanks for the, It works perfectly ;) – Suneth Kalhara Dec 31 '17 at 08:59
-1

Use preg_replace to flip the day and month on the incoming dates, then use date as you want.

$orig = ['14/12/17', '30/11/17', '20/11/17'];

foreach ($orig as $s) {
    echo date('d-m-Y', strtotime(preg_replace('#(\d+)\/(\d+)\/(\d+)#', '$2/$1/$3', $s)))."\n";
}
Will Hines
  • 319
  • 2
  • 12