在用户表单输入日期之后计算星期六

时间:2013-04-14 07:14:44

标签: php date strtotime

我已经制作了一个表格,用户可以从压缩机中选择日期。它将返回d/m/Y,2013年5月14日。

我需要的是星期六跟进日期14/05/2013将于18/05/2013

日期字段名为:$_POST['field_3']

我一直在与strtotime挣扎但没有成功

我已经做过了声明:

<?php

$today = $_POST['field_3'];

$date = strtotime('d/m/Y','next Saturday', $today);

$initialString =  date('m/d/Y', $date);

$end = date('m/d/Y', strtotime( 'next saturday 11:59 pm', $date));

echo $today ."<br>";
echo $initialString . ' - ' . $end;

?>   

返回:

  

14/05/2013

     

01/01/1970 - 01/03/1970

1 个答案:

答案 0 :(得分:1)

非常基本,但这可以提供帮助:

<?php

$year               = 2013; // use substr() (or other stuff) to set these variables
$month              = 5;
$day                = 14;

$newDate    = mktime(0, 0, 0, $month, $day, $year); // creates a date with previous variables

$dayOfWeek  = date('w', $newDate);                  // get the weekday number; 0 = sunday, ..., 6 = saturday

$numberOfDaysTillNextSaturday   = (6 == $dayOfWeek) ? 7 : (6 - $dayOfWeek); // how many days until next saturday ? If saturday = 6, otherwise = (Saturday - weekday)

$nextSaturdayDate   = $newDate + (86400 * $numberOfDaysTillNextSaturday);   // creates a new date corresponding to next saturday

$nextSaturdayString = date("d/m/Y", $nextSaturdayDate);                     // formats the new date as (day)/(month)/(year)

echo $nextSaturdayString;                                                   // echoes the string
?>