Php 将日期添加到日期变量
我正在尝试为从数据库中提取的日期添加天和/或周。我得到的只是1969年12月31日的默认日期,它无法正确输出。这是我的密码:Php 将日期添加到日期变量,php,mysql,date,strtotime,Php,Mysql,Date,Strtotime,我正在尝试为从数据库中提取的日期添加天和/或周。我得到的只是1969年12月31日的默认日期,它无法正确输出。这是我的密码: $lastFeed = "6-25-2013"; //pulled from database last feed date $feedSchedule = "2"; //pulled from database number of weeks. $nextFeeding = date("m-d-Y", strtotime($lastFeed . ' + ' . $
$lastFeed = "6-25-2013"; //pulled from database last feed date
$feedSchedule = "2"; //pulled from database number of weeks.
$nextFeeding = date("m-d-Y", strtotime($lastFeed . ' + ' . $feedSchedule. ' week'));
我还尝试将天数乘以$feedSchedule变量,并将周替换为天。
6-25-2013
不是有效的日期格式。请尝试YYYY-MM-DD
以下代码将起作用并解释无效的
很抱歉,这是一个输入错误,我已将其编辑为$lastFeed。我正在尝试将天/周添加到特定的提取时间,而不是您的链接建议的今天的日期。您是否回显/打印您检索到的
$lastFeed
变量,以确保您的年份不会被检索为0000,而返回的是12-31-1969I。我只是尝试使用格式化它,看看它是否会输出相同的日期(“m-d-Y”,strotime($lastFeed));我得到的是1969年的日期。我使用的日期格式很好,而且它的用途也很好。我以前和以前在这个应用程序的代码行中使用过它。@n\u starnes,但正如您所观察到的,它不能作为strotime()
的输入格式。您不必全局切换格式,但需要对其进行修改,以便与strotime()
一起使用。按照您的建议进行操作会显示:1969年12月Wed我需要的是准确的日期信息,而不是一周中的哪一天。对不起,我忘了在第三行加上分号。。。刚刚在PHP沙盒中试用过,一切都很好!我把分号放在那里,因为我也注意到了。它仍然不起作用。请在运行转换之前尝试此操作echo$lastFeed
并让我确切地知道您得到了什么。我正在运行此操作,而不进行转换$lastFeed在回显时显示6-25-2013的正确格式,然后在函数中使用$lastFeed。函数运行后,显示日期为1969年12月31日$feedschedule每次使用都会有所不同。在一只动物身上,它是2,在另一只动物身上,它可以是10。我确实输入了日期和原始数字,而不是一个变量,它返回的很好。
function nextFeeding($lastFeed,$feedSchedule){
//fix date format
$correctedDate = explode("-",$lastFeed);
//pad month to two digits may need to do this with day also
if($correctedDate[0] < 10 && strlen($correctedDate[0])!==2){
$correctedDate[0] = "0".$correctedDate[0];
}
$correctedDate = $correctedDate[2]."-".$correctedDate[0]."-".$correctedDate[1];
//get the next feeding date
$nextFeeding = date("m-d-Y", strtotime($correctedDate . ' + ' . $feedSchedule. ' week'));
//return value
return $nextFeeding;
}
$lastFeed = "6-25-2013"; //pulled from database last feed date
$feedSchedule = "2"; //pulled from database number of weeks.
$nextFeeding = nextFeeding($lastFeed,$feedSchedule);
echo $nextFeeding;
07-09-2013