当请求php脚本生成的图像时,如何使用javascript/其他方法以某种格式打印日期
How to use javascript/other method in order to print the date in a format, when requesting an image generated by a php script?
我正在使用bootstrap创建一个网站,以提供海滩的天气更新。我的网站
以下php脚本请求正确的图像。
http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from=20130821
最后一组数字是倒过来的日期。
基本上,我希望这个图像在页面中更新,并成为正确的日期。我试着使用javascript,但我无法让它工作
var var d = new Date();
var n = d.getDate();
这是网站的源代码:
<div class="col-lg-6">
<h3>Tides</h3>
<p>Tides over the next 2 days</p>
<img src="http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from=20130821" class="img-responsive" alt="Responsive image">
</a>
</div>
我不知道如何使用javascript将img src更改为正确的日期
编辑我现在有,我仍然无法使它工作
<div class="col-lg-6">
<h3>Tides</h3>
<p>Tides over the next 2 days</p>
<img src="http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from=20130821" class="img-responsive" alt="Responsive image">
<script>
var d = new Date();
var date = d.getFullYear() + '' + (d.getMonth()+1) + '' + d.getDate();
var image = document.getElementsByClass("img-responsive")[0];
image.src = 'http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from='+date;
</script>
</a>
</div>
为什么不在服务器上处理此问题?它将非常简单,只需将CCD_ 1放置在图像url中即可:
<img src="http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from=<?=date('Ymd');?>" class="img-responsive" alt="Responsive image">
我的javascript知识并不是很好,但getDate()只返回当天的数字,例如今天的21。一个非常基本的例子是自己组合:
var d = new Date();
var date = d.getFullYear() + '' + (d.getMonth()+1) + '' + d.getDate();
也许有更好的解决方案,也许有某种格式化程序,但我对javascript不太了解,所以我不知道有什么。(此外,值得注意的是,javascript中的月份从0-11不等,但我真的无法解释为什么会这样,以及如果你想这样做的话,为什么日期从1开始而不是0不一致…)
有关Date对象的更多信息,请点击此处:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
然后要更改图像,您可以执行以下操作:
var image = document.getElementsByClass("img-responsive")[0];
image.src = 'http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from='+date;
在您想要更改的函数中。
相关文章:
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 如何从其他方法访问Angular.js$scope
- 有没有一种方法可以在过滤掉iOS Chrome、Firefox和其他浏览器的同时检测iOS safari
- 停止内部事件处理程序冒泡的其他方法,jQuery
- 有没有办法将选择器返回到元素?是$(这个)还是其他方法
- 比使用大量其他 if 语句更有效的方法
- 使用一个方法在c#中启动其他方法
- 有没有一种方法可以捕获JavaScript控制台输出,以便我可以在其他地方引用它
- Meteor-如何从其他JS文件中调用外部类方法
- 使用appendchild或其他方法向ul添加html代码而不是纯文本
- 如何从指令中的其他模块调用方法
- 使用map方法将javascript对象数组转换为其他对象数组
- 用Javascript或其他方法通过语音读取整个HTML页面(离线)
- 有没有其他方法可以在远程服务器上包含php文件?除了<?php include()>
- 为什么不't我的Javascript,如果其他方法有效的话
- 有没有办法使用画布或其他方法在 HTML 中制作圆形图像
- Javascript 调用其他对象方法
- 在其他 HTML 页面中调用方法
- 如何在 emberjs 中访问其他控制器方法
- 无法获得其他方法是否可以在 HAML JavaScript 中正常工作