0

I try to convert an javascript date with php to Y-m-d but it's not working proper.

$to = 'Wed Jul 01 2020 15:09:00 GMT 0300 (Eastern European Summer Time)';
$to = substr($to, 0, strpos($to, '('));
$to = date('Y-m-d',strtotime($to));

echo $to;

result is:0300-07-04

executable
  • 3,365
  • 6
  • 24
  • 52
H3llDown
  • 21
  • 2
  • With only `Jul 01 2020 15:09:00` in `$to` the strtotime will return the right date. Maybe try to use some Regex to get only that part Or adjust your substr function – Baracuda078 Sep 17 '20 at 13:25
  • 1
    I don't understand the JavaScript part ? The output you're looking for is 2020-07-01 ? – executable Sep 17 '20 at 13:30
  • @executable what their $to contains, is the default format you get when a JS `Date` object is put into a string context. Other than that this is their input format, the question does not appear to have anything to do with JS. – CBroe Sep 17 '20 at 13:31
  • and the q don't deserve -1 as well , u guys give -1 to each and every question – aryanknp Sep 17 '20 at 13:35
  • @aryanagarwal problems such as this could do with a bit more attention (on the part of the person having them), than just “tried something, did not work, now you make for me.” What date formats strtotime _can_ understand, is documented. – CBroe Sep 17 '20 at 13:41

1 Answers1

0

I changed the substr so it will get the right part of the date string that can be ussed in strtotime(). Maybe some one know a better way because using this doent feel right in my opinion.

$to = 'Wed Jul 01 2020 15:09:00 GMT 0300 (Eastern European Summer Time)';
$to = substr($to, 3, strpos($to, 'GMT'));
$to = date('Y-m-d', strtotime($to));

echo $to;
Baracuda078
  • 677
  • 1
  • 5
  • 10