使用“日期”和“日期”获取日期的问题PHP中的'strtotime'功能

时间:2013-04-10 07:36:35

标签: php date strtotime

我使用PHP中的日期函数与日期分开获取日,月和年。 我在这个函数中传递了两个日期,一个是fromdate而另一个是todate。 在todate的情况下,函数返回适当的值但是在fromdate的情况下,函数分别返回0,1,2表示月,日,年。

以下是我在PHP中使用的代码:

// here i am passing values for fromdate and todate fetched from POST, these values are proper

$from = $_POST['event_fromdate'];
$to=$_POST['event_todate'];

// here i am fetching month,day & year separately from fromdate , these values are not proper 

$time['month'] = date('m',strtotime($from));
$time['day'] = date('d',strtotime($from));
$time['year'] = date('Y',strtotime($from));

// here i am fetching month,day & year separately from todate , these values are proper

$time1['month'] = date('m',strtotime($to));
$time1['day'] = date('d',strtotime($to));
$time1['year'] = date('Y',strtotime($to)); 

在逐个打印这些值时,输出为:

Actual From date: 13-APR-2013
Actual To date  : 14-APR-2013

'From date' fetched from date function:
  month = 0
  day = 1
  year = 2


'To date' fetched from date function:
  month = 04
  day = 14
  year = 2013

在这里,我已经尝试了所有方式,但没有得到为什么我在FROM DATE的情况下得到不适当的结果。

1 个答案:

答案 0 :(得分:0)

如果你只是将它放在DateTime对象中,你可以从中提取所有值,为你节省大量的重复代码。

$dt_to     = new DateTime($to);
$some_info = array(
  'day'   => $dt_to->format('d'),
  'month' => $dt_to->format('m'),
  'year'  => $dt_to->format('Y')
);

然而,只需将DateTime对象传递到您实际需要日/月/年的端点,这将是一个更清晰的解决方案。

相关问题