【问题标题】:Convert date string PHP [duplicate]转换日期字符串PHP [重复]
【发布时间】:2020-01-10 11:02:52
【问题描述】:

我在$datum = $_COOKIE['ingevoerde_datum'];下面找到了字符串:

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

我将其剥离为: 2020/01/10

我是这样做的:

$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;

现在我想把它转换成日期, 在网上找到了这个:

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

但这给了我: Thu/Jan/1970 01:00:00

我做错了什么?

【问题讨论】:

  • 尝试删除这一行:$streepje = str_replace('-', '/', $datum_zonder_begin); - 2020-01-10 是一个有效的日期字符串(无论如何对于strtotime())...我不确定2010/01/10 是。
  • 没有任何作用
  • 您附加到日期字符串的随机 &lt;br /&gt; 也没有任何好处...

标签: php string date variables strtotime


【解决方案1】:

只需执行explode(),删除配额,然后使用字符串中的原始日期:

<?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));
?>

输出:

Fri/Jan/2020 15:00:00

当然,如果您需要一些不同的输出,您可以使用date() 格式。

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多