0

I have date stamps from external source as per below format

Tue 6/02/2018

However when trying to convert the date to Y-m-d for SQL format the date get skewed, using the below;

date("Y-m-d H:i:s", strtotime('Tue 6/02/2018');
//ends up being Y-d-m

I have set the timezone earlier using

date_default_timezone_set('Australia/Brisbane'); 
BENN1TH
  • 2,003
  • 2
  • 31
  • 42

1 Answers1

1

strtotime() cannot parse the format "Tue 6/02/2018" correctly. You have to use DateTime::createFromFormat() to parse from your format.

$time = 'Tue 6/02/2018';
$date = DateTime::createFromFormat("D j/m/Y", $time) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 07:12:19

You also could use new DateTimeZone() as:

$time = 'Tue 6/02/2018';
$tz = new DateTimeZone('Australia/Brisbane');
$date = DateTime::createFromFormat("D j/m/Y", $time, $tz) ;
echo $date->format("Y-m-d H:i:s") ; // 2018-02-06 17:17:48
Syscall
  • 19,327
  • 10
  • 37
  • 52