DateTime没有解析到目前为止的字符串

时间:2012-08-30 11:45:29

标签: php sugarcrm

我正在使用DateTime构造函数来构造日期,但它没有解析,我的代码正在跟随

  $current=$timeDate->nowDate(); 

//$timeDate->nowDate() is sugarCRM function returns date and in next line i fetched variables $y, $m, $d successfully 

  list($y, $m, $d) = explode("/", $current);
  $expiredate = new DateTime($y.'-'.$m.'-'.$d);

在异常中捕获到错误

DateTime::__construct() [datetime.--construct]: Failed to parse time string (08-30-2012) at position 0 (0): Unexpected character

2 个答案:

答案 0 :(得分:4)

变量的顺序($ y,$ m,$ d)似乎已关闭,你应该写

list($m, $d, $y) = explode("/", $current);

答案 1 :(得分:1)

如果您正在寻找更简单的方法,可以使用:

$current = new DateTime('now');
$expiredate = $current->format('Y-m-d');