0

I had the string beneath out of $datum = $_COOKIE['ingevoerde_datum'];:

[[52,2,"2020-01-10 15:00:00",0]]

I stripped it to: 2020/01/10

I did that by doing this:

$datum_zonder_eind = substr_replace($datum, '', -14) . "<br />\n";
$datum_zonder_begin = substr($datum_zonder_eind, strpos($datum_zonder_eind, "_") + 8);    
$streepje = str_replace('-', '/', $datum_zonder_begin);
echo $streepje;

Now I want to convert it to a date, found this online:

$input = $streepje; 
$date = strtotime($input); 
echo date('D/M/Y H:i:s', $date);

But this gives me: Thu/Jan/1970 01:00:00

What am I doing wrong?

LTCode
  • 35
  • 6
  • Try deleting this line: `$streepje = str_replace('-', '/', $datum_zonder_begin);` - **2020-01-10** is a valid date string (for `strtotime()` anyway) ... I'm not sure that **2010/01/10** is. – CD001 Jan 10 '20 at 11:05
  • Does not have any effect – LTCode Jan 10 '20 at 11:07
  • The random `
    ` you'd appended to the date string wasn't doing it any favours either...
    – CD001 Jan 10 '20 at 11:14

1 Answers1

-1

Simply do explode(), remove the quotas and then use the original date from the string:

<?php
    $str = '[[52,2,"2020-01-10 15:00:00",0]]';
    $strArray = explode(',', $str);
    $strDate = str_replace('"', '', $strArray[2]);
    echo $date = date('D/M/Y H:i:s', strtotime($strDate));
?>

Output:

Fri/Jan/2020 15:00:00

Of course, you can play with date() formats if you need a bit different output.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31