如何获得星期几的最接近日期?

时间:2018-09-25 13:04:17

标签: php laravel date

我的工作日为1到7(星期一至星期日)。

我想获取下一个日期,其中日期是指定的日期(从1到7)。

例如,今天是25.09.2018。

当我通过5(星期五)时,我想获得28.09.2018,这是下一个最接近的星期五。

如何实现?预先感谢!

6 个答案:

答案 0 :(得分:7)

星期五(5)示例

Carbon::now()
    ->next(5)
    ->toDateString();

More about Carbon library

答案 1 :(得分:3)

请参见strtotime()

`strtotime('next tuesday');`

您可能会发现使用strtotime:

echo date("jS F, Y", strtotime('next friday'));

答案 2 :(得分:2)

这是您可以在任何地方使用的自定义通用函数,

function getNextDate($date, $number)
{
    $numericDay = (($number - 1) <= 0 ? 0 : $number - 1);
    $day        = jddayofweek($numericDay, 1);
    echo date('d.m.Y', strtotime("next " . $day . "", strtotime($date)));
}
getNextDate("25.09.2018", 5);

您也可以将该函数用作通用函数。

您将清楚了解jddayofweek

我通过了1作为包含星期几(英语-格里高利安)的Return字符串。

我将1减去1,因为默认情况下,星期一被视为第一个索引。

答案 3 :(得分:1)

不使用碳素

Real memory size (in bytes) used to set maximum heap size

答案 4 :(得分:1)

  • 找到给定日期的星期几
  • 计算要添加的天数。首先,找到到达周末的天数(7-当前工作日数)。然后,添加给定的输入。
  • 使用datestrtotime函数来获取结果日期。

尝试以下操作(在代码注释中进行解释):

// Get weekday number for current date (or, you can use any other given date)
$start_date = time();
$weekday = (int)date('w', $start_date);

// assuming $input eg: 5 (Friday)
// calculating the differential we need to add 
$diff = (7 - $weekday) + $input;

$new_date = date(strtotime($start_date . "+" . $diff . " day"));

答案 5 :(得分:1)

通过react-native.cmd run-android ,您可以像这样的下一个星期五strtotime

因此在PHP中类似的方法可能起作用:

strtotime('next friday')