我需要一周的第一天(星期一),8周后的今天,其中8是一个变量。
在PHP中,最好的方法是什么?
发布于 2016-11-29 11:00:45
$weeks = 8;
// Timestamp for $weeks weeks ago
$time = strtotime("$weeks weeks ago");
// Day of the week for $time (1 - Mon, ...)
$week_day = date('N', $time);
// Number of days from Monday
$diff = $week_day - 1;
// The date of the Monday $weeks weeks ago
echo date('j', $time - ($diff * 24 * 3600));发布于 2016-11-29 11:21:52
你可以这样做
echo date("l M-d-Y", strtotime('monday this week'));
echo date("l M-d-Y", strtotime('sunday this week'));
echo date("l M-d-Y", strtotime('monday last week'));
echo date("l M-d-Y", strtotime('sunday last week'));
echo date("l M-d-Y", strtotime('monday next week'));
echo date("l M-d-Y", strtotime('sunday next week'));你也可以按月搜索。
echo date("l M-d-Y", strtotime('first day of this month'));
echo date("l M-d-Y", strtotime('last day of this month'));
echo date("l M-d-Y", strtotime('first day of last month'));
echo date("l M-d-Y", strtotime('last day of last month'));
echo date("l M-d-Y", strtotime('first day of next month'));
echo date("l M-d-Y", strtotime('last day of next month'));发布于 2016-11-29 10:53:19
您可以使用PHP中的日期进行一些数学计算,例如:
$now = date('F d, Y H:i');
$newdate = date('F d, Y H:i', strtotime($now.' - 8 weeks'));
echo $newdate;在这种情况下,它将输出当前日期减去8周。
此外,要计算哪一天是今天,您可以使用:
$dw = date( "w", strtotime($newdate));其中$dw为0(周日)到6(星期六),可以找到更多的信息:PHP:日期
溶液
就您的情况而言,如下所示:
<?php
$weeks = 8;
$now = date('F d, Y H:i:s');
$newdate = date('F d, Y H:i:s', strtotime($now.' - '.$weeks.' weeks'));
$new_date_day = date( "w", strtotime($newdate));
$minus = $new_date_day - 1;
if ($minus < 0) { //check if sunday
$plus = $minus * -1;
$newdate = date('F d, Y H:i:s', strtotime($newdate.' + '.$plus.' days'));
} else {
$newdate = date('F d, Y H:i:s', strtotime($newdate.' - '.$minus.' days'));
}
echo $newdate;
?>当然,你可以echo任何形式的约会,你想要的。F.ex。F d, Y H:i:s将输出November 28, 2016 06:18:03。
https://stackoverflow.com/questions/40863944
复制相似问题