【问题标题】:strtotime conversionstrtotime 转换
【发布时间】:2012-06-06 23:01:32
【问题描述】:

如何将这个来自数据库的 str 格式化为更易读的内容。

2012-04-06T10:55:58-07:00

我想要 m-y 的格式。 我试过了

$date = date('m-y',strtotime('2012-04-06T10:55:58-07:00'));

我被难住了。

谢谢!

【问题讨论】:

  • 您应该从数据库中导出为 unix 时间戳,或者在查询中执行日期格式设置。 mysql?
  • 数据来自 Microsoft crm。我们在恢复数据方面非常有限。
  • 你说你想要 m-y 的形式,但是在函数中它的 y-m?您期望 $date 的值是多少?
  • 问题是您没有日期/时间字符串,而是查看时间部分的间隔。每条记录都是间隔吗?你可以选择去掉最后的 x 个字符,虽然它不是很可靠......
  • @Tom,好的,你希望 $date 是什么?

标签: php


【解决方案1】:

如果时区没有意义,你可以这样去掉:

$dbDateString = '2012-04-26T10:55:58-07:00';
$dateString = substr($dbDateString, 0, 10);
$date = date('m-y',strtotime($dateString);

或者你可以走便宜的路线:

$dbDateString = '2012-04-26T10:55:58-07:00';
$year = substr($dbDateString, 0, 4);
$month = substr($dbDateString, 5, 2);

【讨论】:

    【解决方案2】:

    查看:How do I convert an ISO8601 date to another format in PHP?

    他们提供的解决方案是:

    $date = '2011-09-02T18:00:00';
    
    $time = strtotime($date);
    
    $fixed = date('l, F jS Y \at g:ia', $time); // 'a' is escaped as it's a format char.
    

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 2011-05-29
      • 1970-01-01
      • 2011-11-20
      • 2021-04-30
      相关资源
      最近更新 更多