php – 如何根据json显示的日期和时间显示json收到的字符串元素?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何根据json显示的日期和时间显示json收到的字符串元素?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2032字,纯文字阅读大概需要3分钟。
内容图文
![php – 如何根据json显示的日期和时间显示json收到的字符串元素?](/upload/InfoBanner/zyjiaocheng/824/ee1a53dff91248a4a4fa841db23075b6.jpg)
我有一个数据库,我在那里存储一个文本,它的持续时间和它应该出现在网页上的时间.
php的结果如下:
{"text_content":"dgsgdsgds","text_duration":"15","start_time":"2015-09-28 23:11:15"},{"text_content":"dgsgdsgds","text_duration":"15","start_time":"2015-09-28 23:11:30"},{"text_content":"gdsgdsgds","text_duration":"15","start_time":"2015-10-01 14:00:00"}
我有一个jquery脚本从数据库中提取数据并将其打印在屏幕上:
var results =[];
var cursor = 0;
function myFunction () {
$.getJSON('list2.php', function(json) {
results = json;
cursor = 0;
// Now start printing
printNext();
});
}
function printNext(){
if(cursor == results.length){
// Reset the cursor back to the beginning.
cursor = 0;
}
// Print the key1 in the div.
//$('#mydiv').html(results[cursor].key1);
$('#mydiv').hide('fast', function(){ $('#mydiv').html(results[cursor].text_content); $('#mydiv').show('fast'); });
// Set a delay for the current item to stay
// Delay is key2 * 1000 seconds
setTimeout(function(){
printNext();
}, results[cursor].text_duration * 1000);
// Advance the cursor.
cursor++;
}
现在我想添加一个文本在屏幕上显示的功能仅在从数据库获取的日期,作为start_time,但我不确定是否可以仅在jquery上执行,而无需进一步访问服务器代码..
我尝试添加一些if语句,如:
if(results[cursor].start_time == new Date()){
就在将它打印在屏幕上之前,但它没有做到这一点.你能帮帮我吗?
谢谢!
解决方法:
用JSON.parse解析你的json字符串:
var myObj = JSON.parse(results[0]);
并比较你的start_time:
if (new Date(myObj.start_time) == new Date())
如果你想在特定的时间运行你的功能使用setTimeout:
var diff = new Date(myObj.start_time).getTime() - new Date().getTime();
setTimeout(function() {
$('#mydiv').hide('fast', function() {
$('#mydiv').html(results[cursor].text_content);
$('#mydiv').show('fast');
});
}, diff)
或者您可以使用setInterval每1000ms执行一次函数并使用clearInterval将其停止:
function check() {
if (new Date(myObj.start_time) == new Date()) {
$('#mydiv').hide('fast', function() {
$('#mydiv').html(results[cursor].text_content);
$('#mydiv').show('fast');
});
clearInterval(myInterval);
}
}
var myInterval = setInterval(check, 1000)
内容总结
以上是互联网集市为您收集整理的php – 如何根据json显示的日期和时间显示json收到的字符串元素?全部内容,希望文章能够帮你解决php – 如何根据json显示的日期和时间显示json收到的字符串元素?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。