我在制作一个简单的PHP函数以在我的Web空间上工作时遇到了一些问题,同时它在我的localhost服务器上也能正常工作。
问题在于以下计算:
echo $Formated = date("Ymd",strtotime("last day of next month"));
这个脚本似乎不起作用b/c当我在服务器上运行它时,我只是得到了默认日期19691231
,而不是正确的日期20110630
。
我使用windows(XAMP)作为本地主机服务器,所以我想这两个平台的处理方式中一定存在某种形式的问题?
strtotime
是出了名的跨版本问题,所以我建议进行大规模简化。您可以在日期格式中使用"t"字符来表示一个月的最后一天,然后减少strtotime调用,只需返回下个月的时间戳。
echo $Formated = date("Ymt", strtotime("next month"));
如果月份名称未给定,则不要使用of
尝试
date('m/d/y', strtotime('last day next month'));
OR
date('m/d/y', strtotime('last day of march')); // give the month name with of
参考
忘记迁移和部署,事实上strtime
是不可靠的。浏览PHP的官方网站:查看strtotime手册,尤其是这条评论。
如果您有可用的MySQL连接,SELECT DATE_ADD("2011-05-31",INTERVAL 1 MONTH)将不那么冗余,因为(正确的)功能已经实现,而无需您自己实现。