1

As ive echoed out the datetime value from my db, i am now trying to display this value to edit it in the datetime-local field within my form. The datetime vaue from db is set to: 22/3/2017 10:00:00

however, after attempting to use the following code, im left with this: 1970-01-01T01:00:00

 $dat = date("Y-m-d\TH:i:s", strtotime($_GET["dat"]));

How & why is this function not working correctly to display '22/3/2017 10:00' in the form field?

sanitizer
  • 95
  • 3
  • 14
  • Your date format is incorrect. There is no 22nd month. See the above duplicate for how to handle these date formats. – John Conde Mar 22 '17 at 13:54
  • has `$dat=date('Y-m-d\TH:i:s', strtotime('22/3/2017 10:00:00'));` same result? – vp_arth Mar 22 '17 at 13:54
  • The second paramater should be an integer, more specifically a unix timestamp. If none is provided, the current timestamp will be used. As it currently stands `strtotime` will result in an error. Long story short, the input of `strtotime` is wrong. – Andrei Mar 22 '17 at 13:58
  • Thanks all! vp_arth answer solved it – sanitizer Mar 22 '17 at 14:33

2 Answers2

2

Use DateTime::createFromFormat:

$date =  DateTime::createFromFormat('d/m/Y H:i:s', '22/3/2017 10:00:00');
$dat = $date->format('Y-m-d\TH:i:s');
echo $dat;

Your code is not working because strtotime makes assumption based on delimiters about actual format:

  • m/d/Y- American format
  • d.m.Y or d-m-Y - European
vp_arth
  • 14,461
  • 4
  • 37
  • 66
0

It's not working because strtotime() can translate only specific date format.

Check the manual. For a list of supported date format, look here.

Your date format looks not included in the supported ones to me.

Examples:

strtotime("03/22/2017 10:00:00"); // Works: returns 1490173200
strtotime("22/3/2017 10:00"); // Doesn't work: returns false

You have to either change the date format in your DB or format it to one of the supported formats to make it work.

napolux
  • 15,574
  • 9
  • 51
  • 70