简体   繁体   English

如何使用带有php变量的javascript getTime()?

[英]How can to use javascript getTime() with php variable on it?

Im trying yo compare two dates, 1 is the date today second is the date based in my database(SQL Query). 我正在尝试比较两个日期,1是今天的日期,第二是基于数据库(SQL查询)的日期。 They told me to use new Date() but I'm confused how can I insert php variable there. 他们告诉我使用new Date()但是我很困惑如何在其中插入php variable So here's my code: 所以这是我的代码:

<script type="text/javascript">

var today = new Date();
var expired = new Date(today);

function myDIV() {
  if (today >= expired) {
    document.getElementById("myDiv").style.backgroundColor="#ff4d4d";
  }
}
</script>

What I'm trying to do is whenever the second date is the same date as today, the div will change it's color. 我想做的是每当第二个日期与今天相同时,div就会更改其颜色。 So I've tried using >= and == but no luck. 所以我尝试使用>===但没有运气。 Also I tried to do this: 我也尝试这样做:

<script type="text/javascript">

var today = new Date();
var expired = new Date($passport_expiration);

function myDIV() {
  if (today.getTime() >= expired.getTime()) {
    document.getElementById("myDiv").style.backgroundColor="#ff4d4d";
  }
}
</script>

The format of the date is mm/dd/Y 日期格式为mm/dd/Y

<script type="text/javascript">

var today = new Date();
var expired = new Date("<?php echo $passport_expiration; ?> ");
alert(expired);
function myDIV() {
  if (today.getTime() >= expired.getTime()) {
    document.getElementById("myDiv").style.backgroundColor="#ff4d4d";
  }
}
</script>

Working copy 工作副本

<?php 
#$passport_expiration = "07/12/2016";
$passport_expiration = "07/18/2016";
?>
<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <script type="text/javascript">
            var today = new Date();
            var expired = new Date("<?php echo $passport_expiration; ?> ");
            alert(today.getTime() >= expired.getTime());
        </script>
    </body>
</html>

Oh dates are fun! 哦,约会很有趣! There's a few things to think about. 有一些事情要考虑。

  • Is the PHP date the local date, or UTC? PHP日期是本地日期还是UTC?
  • If you're using getTime() from JavaScript you're getting the number of milliseconds since 1970, so any date today won't equal your date from PHP which will convert to the time at midnight. 如果您使用的是JavaScript中的getTime() ,那么您将获得自1970年以来的毫秒数,因此今天的任何日期都不会等于您的PHP中的日期,该日期将转换为午夜的时间。
  • Be careful with two digit years. 请注意两位数的年份。 Although you're probably safe for another 80 years or so. 尽管您可能再安全80年左右。
  • You said in the question you need to know when the days match . 您在问题中说过,您需要知道什么时候匹配 Could it just be when expired is less than right now? 难道只是到期时间比现在短? If not, you need to 'round down' the current time to the nearest day. 如果不是,则需要将当前时间“舍入”到最近的一天。 I've broken it all apart so you can more easily go an look up the functions if you want. 我已将其分解,因此您可以根据需要更轻松地查找功能。

 var testDate = '7/16/16'; var today = new Date(); var month = today.getMonth() + 1; var day = today.getDate(); var fullYear = today.getFullYear() - 2000; var todayString = month + '/' + day + '/' + fullYear; console.log(todayString); console.log(testDate); console.log(todayString === testDate); 

If it's all a bit daunting and you'll need to be working with JavaScript dates a lot, check out momentjs (although it's relatively huge if that's a concern). 如果这有点令人生畏,并且您需要大量使用JavaScript日期,请查看一下momentjs (尽管这是一个相对较大的问题)。

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

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