0

我正在使用 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
4

2 回答 2

4

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

list($m, $d, $y) = explode("/", $current);
于 2012-08-30T11:50:21.877 回答
1

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

$current = new DateTime('now');
$expiredate = $current->format('Y-m-d');
于 2012-08-30T12:04:44.500 回答