简体   繁体   English

获取当前MYSQL时间戳并从24小时减去它

[英]Take Current MYSQL Timestamp And Subtract It From 24 Hours

One thing that has always confused me is Time in coding. 让我困惑的一件事是编码时间。 I am very confused on how to go about doing this! 我对如何做到这一点非常困惑! Simply, I have a MySql Timestamp, and I want to subtract that from 24 hours. 简单地说,我有一个MySql时间戳,我想从24小时中减去它。

Basically, I am trying to code something that only lets you complete an action once every 24 hours, and then if they try to complete it more then once it will tell them how long they have to wait to do it again. 基本上,我正在尝试编写只允许您每24小时完成一次操作的内容,然后如果他们尝试完成更多操作,那么一旦它会告诉他们需要多长时间才能再次执行操作。

This is the code I currently have: 这是我目前的代码:

$dailylogincheck = 2015-08-16 13:30:32 //MySQL Timestamp
date_default_timezone_set("America/New_York");
$lastloginbonus = new DateTime($dailylogincheck);
$currenttime = $lastloginbonus->diff(new DateTime(date("Y-m-d H:i:s")));

I don't have to use this code (if there is a better way to do this). 我不必使用此代码(如果有更好的方法来执行此操作)。 So I would just like to subtract the current Timestamp from 24 hours, but I have no clue how to do this in PHP :( Please Help! 所以我只想从24小时中减去当前的时间戳,但我不知道如何在PHP中执行此操作:(请帮忙!

PS I would simply like to display a sentence like the following: You have already done this within the past 24 hours. Please come back in X hours X minutes and try again. PS我只想显示如下句子: You have already done this within the past 24 hours. Please come back in X hours X minutes and try again. You have already done this within the past 24 hours. Please come back in X hours X minutes and try again.

hope this works :-) 希望这工作:-)

   <?php


$dailylogincheck = '2015-08-16 13:30:32';

$end = new DateTime($dailylogincheck);
$end->add(new DateInterval('P1D'));//add 1 day to find end date
$now = new DateTime('now');

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

if($end->diff($now)->days >= 1){
 echo 'go for it'; 
}else{
echo $interval->format('%H hours, %i minutes until you can XXX');

}

As Dagon said, doing it in mysql is the best approach... but to address your php question, DateTime will accept a number of simple modifiers: 正如Dagon所说,在mysql中执行它是最好的方法...但是为了解决你的php问题,DateTime将接受许多简单的修饰符:

<?php

$lastBonus = new DateTime("-1 day"); // Last bonus was exactly 24 hours ago
$canNotGetNewBonus = new DateTime("-1 hour"); // Attempting another bonus 23 hours later
$canGetNewBonus = new DateTime("+1 hour"); // Attempting another bonus 25 hours later

var_dump($lastBonus->diff($canNotGetNewBonus)->days >= 1);
var_dump($lastBonus->diff($canGetNewBonus)->days >= 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM