0

I am creating a site using bootstrap to give weather updates at the beach. MY SITE

The following php script requests the correct image.

http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from=20130821

the last set of numbers is the date backwards.

Basically I want this image to update in the page and be the correct date. I tried using javascript but i couldn't get it to work

var var d = new Date();
var n = d.getDate();

here is the source code for the website:

<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>

I can't work out how I would use javascript to change the img src to the correct date

EDIT I now have and i'm still unable to make it work

<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>
4

2 回答 2

0

My javascript knowledge isn't really that great, but getDate() only returns the number of the current day, for example 21 for today. A really basic example is to combine it yourself:

var d = new Date();
var date = d.getFullYear() + '' + (d.getMonth()+1) + '' + d.getDate();

There might be a better solution, maybe with some kind of formatter, but I'm not that knowledgeable with javascript so I don't know of any on the top of my head. (Also, worth noting is that the months range from 0-11 in javascript, but I can't really explain why that is so and why it's inconsistent that dates starts at 1 and not 0 if you gonna do it that way...)

For more info on the Date object, there's a bit of that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Then to change the image, you'd do something like this:

var image = document.getElementsByClass("img-responsive")[0];
image.src = 'http://www.ntslf.org/files/php/pltdata_tgi.php?port=Liverpool&span=4&from='+date;

in a function where you want to change it.

于 2013-08-21T17:32:40.137 回答
0

Why aren't you handling this on the server? it would be as simple as placing echo date('Ymd'); inside the image 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">
于 2013-08-21T20:24:02.940 回答