简体   繁体   English

PHP中的mktime()输出错误

[英]Wrong output from mktime() in PHP

I am trying to set up a php where that it sets the date up for the 1st September for each year. 我试图建立一个php,它将每年的9月1日设置为日期。 I am using CRON to state that everytime the date is 7th September, then the php will actually update the database for rows whose dates ae a week before that 7th September, (ist September). 我正在使用CRON声明每次日期为9月7日,那么php实际上将更新数据库的日期为9月7日(即9月)一周前的行。

Now the code below I tested for the 28th October and when I echo $selectedDate it outputs 2012-10-28 which is fine. 现在,我在10月28日测试了以下代码,当我回显$selectedDate它输出2012-10-28 ,这很好。 But when I change the date to 1st September, it outputs `2011-12-01 which is obviously incorrect. 但是,当我将日期更改为9月1日时,它会输出`2011-12-01 ,这显然是错误的。 it should output `2013-09-01 as the next September date will be in 2013. Then after the Ist September date has passed in 2013, then the year should change to 2014 and etc. 它应输出`2013-09-01,因为下一个9月日期将在2013年。然后在2013年9月1日这一天过去之后,年份应更改为2014年,依此类推。

How can I get the correct date to be outputted? 如何获得正确的输出日期?

Below is code: 下面是代码:

$createDate = mktime(0,0,0,09,01,date("Y"));
$selectedDate =  date('Y-m-d', ($createDate));

That's because 09 is interpreted as an octal 9 , which is invalid. 这是因为09被解释为八进制9 ,这是无效的。

It works as expected when you pass 9 instead of 09 : 当您传递9而不是09时,它将按预期工作:

$createDate = mktime(0,0,0,9,1,date("Y"));
//                         ^ ^  No preceeding 0s.
$selectedDate =  date('Y-m-d', ($createDate));

var_dump($createDate, $selectedDate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM