2016-06-21 58 views
1

我想在给定日期使用strttotime得到下一个monday,但我从Jan 1970获取日期作为输出。下面是我为下一个星期一的获取日期编写的代码行,我从here得到了这段代码。任何人都可以帮助我理解为什么会发生这种情况。提前致谢。php - 使用strtotime日期到1970年代

代码:

$date_init = date('Y m d', strtotime('next monday', strtotime('2016 06 22'))); 

预期输出:

2016 06 27 

实际输出:

1970 01 05 

回答

5

根据手册,字符串2016 06 22不是有效的日期格式。尝试添加连字符:

$date_init = date('Y m d', strtotime('next monday', strtotime('2016-06-22'))); 

你可以在这里找到所有有效日期格式:http://php.net/manual/en/datetime.formats.date.php

2

试试这个:

$date_init = date('Y m d', strtotime('next monday', strtotime('22-06-2016'))); 

'2016 06 22'需要格式化为上述格式之一here

2

2016 06 02是被接受的日期格式as described in the PHP docs没有之一。由于这个原因,strtotime的内部调用返回FALSEas defined in the docs在给定的时间字符串不能被分析的情况下。

因为这不是外strtotime$now参数的有效输入,需要的时代,或1970年1月1日,作为它的计算基础。

其结果是,你1月1日之后结束了下周一,1970年

从最初的日期删除空格字符串将解决这个问题:

$date_init = date('Y m d', strtotime('next monday', strtotime('20160622')));