您对如何在特定日期之后指定一周的日期有任何想法吗?我正在使用一个日期选择器来获取我的开始时间,以及我如何将其转换为日期。
$formatdate = str_replace("(/\)", "-", $startdate);
$time2 = strtotime( $formatdate );
$date = date('Y-m-d', $time);
我需要知道开始日期1周后的日期才能在我的mysql查询中使用它。就像这样。
SELECT * FROM `table` WHERE `date` BETWEEN '$stardate' AND '$enddate'
谢谢你们!任何帮助都将得到充分的赞赏。
答案 0 :(得分:1)
试试这个
<?php
$currentDate=date("Y-m-d");
$dateOneWeekAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 week");
echo date("Y-m-d",$dateOneWeekAdded);
?>
修改:对于特定日期,您可以将上述代码重写为
<?php
$currentDate="08/14/2012";
$dateOneWeekAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 week");
echo date("Y-m-d",$dateOneWeekAdded);
?>
答案 1 :(得分:1)
$formatdate = str_replace("(/\)", "-", $startdate);
$start = new DateTime( $formatdate );
$startdate = $start->format("Y-m-d H:i:s");
$next_week = clone $start;
$next_week->add(new DateInterval("P1W"));
$enddate = $next_week->format("Y-m-d H:i:s");
$sql = "SELECT * FROM `table` WHERE `date` BETWEEN '$stardate' AND '$enddate'";
答案 2 :(得分:0)
减少代码行数:
$end = new DateTime("today +1 week");
print $end->format('Y-m-d');
对于特定日期:
$start = "2012-10-24";
$end = new DateTime("$start +1 week");
print $end->format('Y-m-d');
答案 3 :(得分:0)
或者你也可以使用MySQL:
SELECT * FROM table WHERE date BETWEEN '$stardate' AND DATE_ADD('$stardate', INTERVAL 7 DAY)