-3

I'm trying to convert string date time to datetime format. bellow is my code

date_default_timezone_set("Asia/Singapore");

$EventDate = '2018-09-14 16:30:00.000';

$createDate = new DateTime($EventDate);
$NewEventDate = $createDate->format('Y-m-d h:m:s');

but when i convert string date to datetime, i get time wrong bellow is the output

STRING EVENT DATE : 2018-09-14 16:30:00.000
CONVERTED EVENT DATE : 2018-09-14 04:09:00

the time 16:30:00 shows 04:09:00 which is wrong

7
  • 3
    Use H instead of h
    – Mohammad
    Oct 11, 2018 at 10:12
  • 5
    Why are you bothering to do all this just to get EXACTLY the same format out as you put in?
    – RiggsFolly
    Oct 11, 2018 at 10:13
  • i tried that also but returns 2018-09-14 16:09:00 the minuets is wrong
    – kanishka
    Oct 11, 2018 at 10:14
  • 1
    i is minutes, no?
    – Script47
    Oct 11, 2018 at 10:14
  • 2
    Please just read the documentation in the manual for all the fomatting characters. It will save you and us a lot of time
    – RiggsFolly
    Oct 11, 2018 at 10:15

3 Answers 3

2

try this

$NewEventDate = $createDate->format('Y-m-d H:i:s');
2
  • 3
    You missed the H not h error
    – RiggsFolly
    Oct 11, 2018 at 10:20
  • @RiggsFolly , thank you for pointing, i updated the answer Oct 11, 2018 at 11:32
1

Use i instead of m for minute

date_default_timezone_set("Asia/Singapore");

$EventDate = '2018-09-14 16:30:00.000';

$createDate = new DateTime($EventDate);
$NewEventDate = $createDate->format('Y-m-d H:i:s');
1
date_default_timezone_set("Asia/Singapore");

$EventDate = '2018-09-14 16:30:00.000';

$createDate = new DateTime($EventDate);
echo $NewEventDate = $createDate->format('Y-m-d H:i:s');

Not the answer you're looking for? Browse other questions tagged or ask your own question.