gpt4 book ai didi

php - 使用 jquery (ajax) 和 php 的上一个和下一个日期

转载 作者:行者123 更新时间:2023-11-29 00:03:14 24 4
gpt4 key购买 nike

我有某种用 php 编写的带有上一个和下一个按钮的日历。现在我想使用一些 ajax 在不刷新页面的情况下转到上一个或下一个日期。

在 ajax 调用的 php 文件中,我有一个查询返回选定日期的任务(类似于:...WHERE date = $date)。

问题是前天等等,后天等等,我都上不了。

谁能帮忙?

编辑

按钮:

<button class='btn btn-default previous' data-date='$date'>
<span class='glyphicon glyphicon-arrow-left' aria-hidden='true'></span>
</button>
<button class='btn btn-default next' data-date='$date'>
<span class='glyphicon glyphicon-arrow-right' aria-hidden='true'></span>
</button>

J查询:

$(document).ready(function() {
$('button.previous').click(function() {
var date = $(this).data('date');
$.ajax({
type: "POST",
data: { action : "previous" , date: date },
url: "includes/ajax/plannerdata.php",
success: function(returnData){
$("#planner-data").html(returnData);
}
});
});
$('button.next').click(function() {
var date = $(this).data('date');
$.ajax({
type: "POST",
data: { action: 'next', date: date },
url: "includes/ajax/plannerdata.php",
success: function(returnData){
$("#planner-data").html(returnData);
}
});
});
});

计划者数据.php:

switch ($_POST['action']) {
case 'next':
$date = strftime('%Y-%m-%d', strtotime($_POST['date'] .' -1 day'));
break;
case 'previous':
$date = strftime('%Y-%m-%d', strtotime($_POST['date'] .' +1 day'));
break;
}

然后我在查询中使用 $date。

更新的 plannerdata.php:

if (!isset ($_POST['date'])) { // At first page load or refresh (without using the previous/next buttons), plannerdata.php is also loaded bij ajax and injected in index.php
$currentDatetime = new DateTime('NOW');
}
else { // Previous or next button is pressed
$currentDatetime = new DateTime($_POST['date']);
}

$dateModification = $_POST['action'];

switch ($dateModification) {
case 'previous':
$currentDatetime->modify('-1 day');
break;
case 'next':
$currentDatetime->modify('+1 day');
break;
}
$date = $currentDatetime->format('Y-m-d');

编辑

plannerdata.php;

if (!isset ($_SESSION['date'])) {
$_SESSION['date'] = new DateTime('NOW');
}

if (isset ($_POST['action'])) {
switch ($_POST['action']) {
case 'previous':
$_SESSION['date']->modify('-1 day');
break;
case 'next':
$_SESSION['date']->modify('+1 day');
break;
}
}

$date = $_SESSION['date']->format('Y-m-d');

按钮(在 index.php 中)。我删除了数据日期属性:

<button class='btn btn-default previous'><span class='glyphicon glyphicon-arrow-left' aria-hidden='true'></span></button>
<button class='btn btn-default next'><span class='glyphicon glyphicon-arrow-right' aria-hidden='true'></span></button>

按钮位于 和两个按钮之间,显示所选日期。

<ul>
<li>button previous</li>
<li><span id="planner_date">selected date</span></li>
<li>button next</li>
<ul>

我还更改了 Jquery。我同时进行了 ajax 调用。

$(document).ready(function() {
$('button.previous').click(function() {
$.when(
$.post("includes/ajax/plannerdata.php", {action: 'previous'}, function(data) {
plannerData = data;
}),
$.post("includes/ajax/date.php", {action: 'previous'}, function(date) {
calendardate = date;
})
).then(function() {
$("#planner-data").html(plannerData);
$("#planner_date").text(calendardate);
});
});
$('button.next').click(function() {
$.when(
$.post("includes/ajax/plannerdata.php", {action: 'next'}, function(data) {
plannerData = data;
}),
$.post("includes/ajax/date.php", {action: 'next'}, function(date) {
calendardate = date;
})
).then(function() {
$("#planner-data").html(plannerData);
$("#planner_date").text(calendardate);
});
});
});

返回当前日期的Date.php:

if (!isset ($_SESSION['current_date'])) {
$_SESSION['current_date'] = new DateTime('NOW');
}

if (isset ($_POST['action'])) {
switch ($_POST['action']) {
case 'previous':
$_SESSION['current_date']->modify('-1 day');
break;
case 'next':
$_SESSION['current_date']->modify('+1 day');
break;
}
}

$date = $_SESSION['current_date']->format('Y-m-d');
echo $date;

编辑 2015-03-05:

Jquery:

$(document).ready(function() {
$('button.previous').click(function() {
$.when(
$.post("includes/ajax/date.php",
{action: 'previous'},
function(date) {
dates = jQuery.parseJSON(date);
calendardate = dates.dutch;

$.post("includes/ajax/plannerdata.php",
{date: dates.sql},
function(data) {
plannerData = data;
})
})
).then(function(){
$("#planner_date").text(calendardate);
$("#planner-data").html(plannerData);
});
});
});

日期.php

if (!isset ($_SESSION['current_date'])) {
$_SESSION['current_date'] = new DateTime('NOW');
}
if (isset ($_POST['action'])) {
switch ($_POST['action']) {
case 'previous':
$_SESSION['current_date']->modify('-1 day');
break;
case 'next':
$_SESSION['current_date']->modify('+1 day');
break;
}
}
$date_1 = $_SESSION['current_date']->format('Y-m-d');
$date_2 = $_SESSION['current_date']->format('d-m-Y');
$response = array('sql' => $date_1, 'dutch' => $date_2);
echo json_encode($response);

2015-03-08 更新

$(document).ready(function() {
$('button.previous').click(function() {
var calendardate;
var plannerData;
$.when(
$.post("includes/ajax/date.php",
{action: 'previous'},
function(date) {
dates = jQuery.parseJSON(date)
calendardate = dates.dutch;

$.post("includes/ajax/plannerdata.php",
{date: dates.sql},
function(data) {
plannerData = data;
})}
)).then(function(){
$("#planner_date").text(calendardate)
$("#planner-data").html(plannerData);
});
});
});

最佳答案

如我所见,最适合您的应该是使用 PHP DateTime .您可以将脚本修改为如下内容:

$dateModification=$_POST['action'];//for clarification
//Datetime understands every format strtotime does
$currentDatetime=new DateTime($_POST['date']);

switch ($dateModification) {
case 'next':
$currentDateTime->modify("+1 day");//simple, isn't it?
break;
case 'previous':
$currentDateTime->modify("-1 day");
break;
}
//final result
$date=$currentDateTime->format("Y-m-d");

使用 DateTime,您可以轻松地将日期作为对象进行操作。也可以比较两个带有 < > == 符号的 Datetime 对象,如下所示:

$firstDatetime=new Datetime("2015-02-20");
$secondDatetime=new Datetime("2015-02-25");
if ($firstDatetime<$secondDatetime)
{
echo '$firstDatetime was indeed first!';
}

亲自尝试一下:)

编辑 - 如何在服务器端处理日期修改:

关于它的更多想法。您更新的代码正在对服务器进行两次 AJAX 调用,它们都在修改 datetime 对象。因为它会起作用,所以它是重复代码。让我们尝试修改调用和脚本以仅处理一个 DateTime 对象。让我们将日期修改移动到您的 date.php 并且 plannerdata.php 将在相同的日期变量上工作。

Javascript:

$(document).ready(function() {
$('button.previous').click(function() {
$.post("includes/ajax/date.php",
{action: 'previous'},
function(date) {
dates = jQuery.parseJSON(date)
$("#planner_date").text(dates.dutch)
$.post("includes/ajax/plannerdata.php",
{date: dates.sql},
function(data) {
$("#planner-data").html(data);
})
})
})
});

PHP:

Date.php 将保持不变,我们必须只更改 plannerdata.php 以使用相同的变量:

//we remove that block, as it will be set in date.php
/*if (!isset ($_SESSION['date'])) {
$_SESSION['date'] = new DateTime('NOW');
}

if (isset ($_POST['action'])) {
switch ($_POST['action']) {
case 'previous':
$_SESSION['date']->modify('-1 day');
break;
case 'next':
$_SESSION['date']->modify('+1 day');
break;
}
}*/

$date = $_SESSION['current_date']->format('Y-m-d');

应该是吧! :)

如何从特定格式创建 DateTime?:

为此目的存在函数DateTime::createFromFormat .用法示例:

setlocale(LC_TIME, "nl_NL");//firstly we need to setlocale, so month names will be properly recognized
$dateString='6 maart 2015';
$datetime=DateTime::createFromFormat("j F Y", $dateString);

这将返回 datetime 对象,您可以对其进行操作。

关于php - 使用 jquery (ajax) 和 php 的上一个和下一个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28748165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com