【问题标题】:Convert string to date using strtotime php [closed]使用strtotime php将字符串转换为日期[关闭]
【发布时间】:2014-06-02 07:20:44
【问题描述】:

请帮助我将字符串转换为日期

我的字符串是$search = '6/2/2014'

我用过这种方法,但它不能正常工作

$time = strtotime('$search');

$newformat = date('DD-MM-YYYY',$time);

echo $newformat;

结果显示为01-01-70这个输出帮助我该怎么办。

【问题讨论】:

  • 使用 $time = strtotime($search); // $search ='6/2/2014';
  • 去掉引号!您的字符串实际上是“$search”,而不是您的 $search 变量。
  • $newformat = date('DD-MM-YYYY', strtotime($search));
  • 他正在使用 strtotime(),只是缺乏知识……为什么要投反对票???

标签: php date strtotime


【解决方案1】:

首先不要使用strtotime,因为它不可靠。使用新的DateTime 类。在你的情况下使用这样的东西:

$time = DateTime::createFromFormat('m/d/Y', $search, new DateTimeZone('America/New_York'));
$newformat = $time->format('d-m-Y');
echo $newformat;

【讨论】:

    猜你喜欢
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多