0

EDIT :

I need to change this datetime into Y-m-d H:i:s format from dd/mm/yy H:i:s format.

This is for PHP or CodeIgniter project.


I got some data from other report not formatted well.

datetime
7/12/17 12:15:23 pm (dd/mm/yy H:i:s) format
8/12/17 5:18:12 am
20/12/17 5:12:24 pm
21/12/17 12:17:37 pm

If I get example using 1st format date.

$example_date = "7/12/17 12:15:23 pm"; // d F Y : 07-December-2017 12:15:23
$format_date = $date("Y-m-d", strtotime($example_date));

Its printed : 2007-12-17 12:15:23 It's a wrong result.

In d F Y date it's printed 17 December 2007 but It's should be 07 December 2017.


Normally we can change date using like this :

$example_datetime = "13/12/2017 12:08:16 pm"; // (dd/mm/yy H:i:s) format
$format_date = date("Y-m-d H:i:s", strtotime($example_datetime));

Result from echo $format_date => 2017-12-13 12:08:16 is correct. If the year is 2017 and this is what I wanted to.

Zinc
  • 385
  • 1
  • 5
  • 19
  • 1
    _“when I used date("Y-m-d H:i:s", strtotime($datetime)); it becomes : 07-12-17 12:15:23”_ - nonsense, that is not even _possible_ with the given format specifier. Plus, https://3v4l.org/jaOGa proves this not to be the case. So, what is the _actual_ issue then? Please edit your question, and include an executable example that shows the problem. – CBroe Jan 30 '18 at 09:02
  • Hi, that format is (dd/mm/yy). So actually that 7 must be date, 12 must be month or December, 17 must be Year or 2017. That link you gave me, its produce 12 as date and 7 as month. – Zinc Jan 30 '18 at 09:15
  • So your question is about _parsing_ given date strings using strtotime, and not about outputting them using date. – CBroe Jan 30 '18 at 09:17
  • I want to ouputing them using date, I'll edit my question.. sorry for confusing. – Zinc Jan 30 '18 at 09:18
  • question edited. please check. – Zinc Jan 30 '18 at 09:32
  • This is still not a problem of _outputting_ the data. Naincy tried to explain to you what the issue is already, and Ramraider’s answer shows you how to avoid it, be _telling_ PHP upfront what format your _input_ data is in. – CBroe Jan 30 '18 at 09:35

2 Answers2

4

You could use the DateTime and createFromFormat method - specifying the format of the input date and then formatting how you wish for output - ex:

$example_datetime = "13/12/2017 12:08:16 pm";
$date=DateTime::createFromFormat('d/m/Y H:i:s a', $example_datetime );

$output=$date->format('Y-m-d H:i:s');
echo $output; //  -> 2017-12-13 12:08:16

For a 2-digit year in the input datestamp, rather than the uppercase Y a lowercase y should match.

$example_datetime = "13/12/17 12:08:16 pm";
$date=DateTime::createFromFormat('d/m/y H:i:s a', $example_datetime );

$output=$date->format('Y-m-d H:i:s');
echo $output; //  -> 2017-12-13 12:08:16
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • can you try to use : `13/12/17 12:08:16 pm` for the output ? thank you. – Zinc Jan 30 '18 at 09:16
  • `Result that I need => 2017-12-13 12:08:16` ?? is the output from above - as expected so I don't understand your comment – Professor Abronsius Jan 30 '18 at 09:20
  • edited question. please check again. thank you. – Zinc Jan 30 '18 at 09:31
  • @zinc if you want to parse a date with a 2-digit year instead of a 4-digit one, then you need to replace the part that specifies the year in the format string accordingly. – CBroe Jan 30 '18 at 09:38
  • @CBroe I will try it again, thank you for your help. I was found that correct answer from RamRaider. I edited his answer. After approved I'll make it correct. – Zinc Jan 30 '18 at 09:39
  • 1
    Note that DateTime::createFromFormat returns FALSE on error. You can fetch the errors with DateTime::getLastErrors() – fudu Dec 14 '20 at 10:37
0

Try This

$example_datetime = "13/12/2017 12:08:16 pm"; // (dd/mm/yy H:i:s) format
$date = str_replace('/', '-', $example_datetime);
echo date("Y-m-d H:i:s", strtotime($date)); // Y-m-d H:i:s

Dates in the m/d/y or d-m-y formats are disambiguate by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. Check more here: http://php.net/manual/en/function.strtotime.php

Naincy
  • 2,953
  • 1
  • 12
  • 21