[英]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>
<?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.
有一些事情要考虑。
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. getTime()
,那么您将获得自1970年以来的毫秒数,因此今天的任何日期都不会等于您的PHP中的日期,该日期将转换为午夜的时间。 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.