2

我制作了一个表格,用户可以在其中从日历中选择日期。它将于 2013 年 5 月 14 日回归d/m/Y

我需要的是 2013 年 5 月 14 日之后的星期六,2013 年 5 月 18 日

日期字段称为:$_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

4

1 回答 1

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
?>
于 2013-04-14T07:39:25.743 回答