检查两个日期之间的差异 [英] Checking the difference between two dates

查看:99
本文介绍了检查两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用if语句来比较两个时间/日期,并查看第二个日期是否在输入第一个日期后的24小时内。如果它不应该允许语句继续



$ date在代码之前设置,是用户输入的日期。

  $ now = new DateTime(); 
$ future_date = new DateTime($ date);

$ interval = $ future_date-> diff($ now);

// echo $ interval-> format(%d days,%h hours,%i minutes,%s seconds);

if($ interval> 60 * 60 * 24){

这里是我试图使用的代码,如果给出的日期是一样的,但我真的不知道如何甚至开始这个。

  if($ interval> 60 * 60 * 24){
pre>

编辑:
i真的不知道它的实际工作,因为无论它刚刚出现一个空白页。

  $ BookID = $ _GET ['BookID']; 
$ id = $ _GET ['id'];

$ query = mysqli_query($ con,SELECT time,date FROM tbl_booking WHERE BookID = {$ BookID}& tbl_mem_id = {$ id});
$ info = mysqli_fetch_assoc($ query);

$ test = $ info ['date']。 $ info ['time'];

$ date = date(Y-m-d H:i:s,strtotime($ test));

$ plus24hours = new DateTime(+ 1 day);
$ future_date = new DateTime($ date);



if(isset($ _ SESSION ['id'])&&$ _SESSION ['id'] == $ _GET ['id']){

if(isset($ _ GET ['BookID'])&& is_numeric($ _ GET ['BookID'])
{

if $ future_date> $ plus24hours){

header(refresh:5; url = dashboard.php);
echo您的预订已被取消。

$ sql =DELETE FROM`tbl_booking` WHERE`BookID` = {$ BookID};
$ result = mysqli_query($ con,$ sql);

}}
} else {
//如果ID未设置,或无效,重定向到查看页
标题(刷新 5; url = dashboard.php);
echo(对不起,剩下不到24小时,你不能取消);


}


解决方案

p>这比你想象的容易得多只需在第一个日期添加24小时,然后将其与未来日期进行比较。如果未来的日期小于第一个日期,将来不到24小时。

  $ plus24hours = new DateTime (+1天); 
$ future_date = new DateTime($ date);

if($ future_date< $ plus24hours){
//未来不到24小时
}


i'm attempting to make an if statement to compare two time/dates and see if the second date is less than 24 hours after the first date entered. if it is it should not allow the statement to continue

$date is set earlier in the code and is the date being inputted by the user.

$now = new DateTime();
$future_date = new DateTime($date);

$interval = $future_date->diff($now);

//echo $interval->format("%d days, %h hours, %i minutes, %s seconds");

if ($interval > 60 * 60 * 24) {

here is the code i am attempting to use to compare if the date being given is the same, but i honestly don't really know how to even start this.

if ($interval > 60 * 60 * 24) {

edit: i don't really know if its actually working because no matter what it just comes up with a blank page.

  $BookID = $_GET['BookID'];
  $id = $_GET['id'];

 $query = mysqli_query($con,"SELECT time, date FROM tbl_booking WHERE BookID={$BookID} && tbl_mem_id={$id}");
$info = mysqli_fetch_assoc( $query);

$test = $info['date'] . $info['time'];

$date = date("Y-m-d H:i:s", strtotime($test));

$plus24hours = new DateTime("+1 day");
$future_date = new DateTime($date);



 if (isset($_SESSION['id']) && $_SESSION['id'] == $_GET['id']) {

 if (isset($_GET['BookID']) && is_numeric($_GET['BookID']))
 {

if ($future_date > $plus24hours) {

header("refresh:5;url=dashboard.php");
echo "Your booking has been cancelled.";   

$sql = "DELETE FROM `tbl_booking` WHERE `BookID`={$BookID}";
$result = mysqli_query($con, $sql);

 }}
 } else {
 // if id isn't set, or isn't valid, redirect back to view page
header("refresh:5;url=dashboard.php");
echo ("Sorry, there is less than 24 hours left and you cannot cancel.");


 }

解决方案

This is easier than you think. Just add 24 hours to the first date and then compare it to the future date. If the future date is less than the first date, it is less than 24 hours in the future.

$plus24hours = new DateTime("+1 day");
$future_date = new DateTime($date);

if ($future_date < $plus24hours) {
    // Less than 24 hours in the future
}

这篇关于检查两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆