如何在 Bootstrap Datepicker 中获取最后一个星期日的日期?
How to get Last sunday date in Bootstrap Datepicker?
我正在使用 bootstrap Datepicker,我想为 current/last 设置日期 Sunday.The 下面的代码显示 $date 到 $date1 的内容形式。
if (!isset($_POST['new-date'])){
$date = date('Y-m-d H:i:s');// I want to set last/current sunday
date here.
$date1 = date("Y-m-d H:i:s", strtotime("$date +6 day"));
$d = explode(" ",$date);
$d1 = explode(" ",$date1);
} else {
$d[0] = $_POST['new-date'];
$d1[0] = date("Y-m-d", strtotime("$d[0] +6 day"));
}
$d[0] = $_POST['new-date'];
['new-date'] 可以是日历中的任何 day/date,但它应该 select 该特定周的最后一个星期日。
您的代码似乎是用 PHP 而不是 JavaScript 编写的,所以我不确定问题标题或标签的相关性如何。
// Check if the day of the week is a Sunday. If yes, use today's date. If no, use last Sunday's date
$timestamp = date('N') == 7 ? strtotime('today') : strtotime('last sunday');
$date = date('Y-m-d H:i:s', $timestamp);
die($date);
编辑
您在评论中询问了如何找到任何给定日期的上周日。
$timestamp = strtotime($_POST['date'] ?? 'today');
$sunday = (date('N', $timestamp) == 7) ? $timestamp : strtotime('last sunday', $timestamp);
$date = date('Y-m-d H:i:s', $sunday);
die($date);
我正在使用 bootstrap Datepicker,我想为 current/last 设置日期 Sunday.The 下面的代码显示 $date 到 $date1 的内容形式。
if (!isset($_POST['new-date'])){
$date = date('Y-m-d H:i:s');// I want to set last/current sunday
date here.
$date1 = date("Y-m-d H:i:s", strtotime("$date +6 day"));
$d = explode(" ",$date);
$d1 = explode(" ",$date1);
} else {
$d[0] = $_POST['new-date'];
$d1[0] = date("Y-m-d", strtotime("$d[0] +6 day"));
}
$d[0] = $_POST['new-date'];
['new-date'] 可以是日历中的任何 day/date,但它应该 select 该特定周的最后一个星期日。
您的代码似乎是用 PHP 而不是 JavaScript 编写的,所以我不确定问题标题或标签的相关性如何。
// Check if the day of the week is a Sunday. If yes, use today's date. If no, use last Sunday's date
$timestamp = date('N') == 7 ? strtotime('today') : strtotime('last sunday');
$date = date('Y-m-d H:i:s', $timestamp);
die($date);
编辑
您在评论中询问了如何找到任何给定日期的上周日。
$timestamp = strtotime($_POST['date'] ?? 'today');
$sunday = (date('N', $timestamp) == 7) ? $timestamp : strtotime('last sunday', $timestamp);
$date = date('Y-m-d H:i:s', $sunday);
die($date);