I have a script to show snow on the website I have just to add this code that calls a js file
<script type="text/javascript" src="snowstorm.js"></script>
Now I would to have this line to work every year from 15 December and stops at 15 January
Example :
If date >15 December && < 15 January { echo '<script type="text/javascript" src="snowstorm.js" ></script>'}
Any help please?
Best regards
Use DateTime
objects:
$now = new DateTime();
if ($now >= new DateTime('December 15') || $now <= new DateTime('January 15')) {
echo '<script src="snowstorm.js"></script>';
}
I'm using logical or ||
here because the script should be shown if the date is before January 15 of this year or if the date is after December 15 of this year.
The condition becomes slightly more complicated with logical and as you would have to account for the different years.
try this
$date = time(); //current time. output timestamp format
date('n')==1?$currentYear=date('Y')-1:$currentYear=date('Y');
if($date > mktime(0, 0, 0, 12, 15, $currentYear) && $date < mktime(0, 0, 0, 1, 15,$currentYear+1))
{
echo '<script type="text/javascript" src="snowstorm.js" ></script>';
}
about php mktime()
Using $.getScript(): we can call script plugin based on condition dynamically.
$.getScript("http://code.jquery.com/jquery-1.9.1.js");
or else you can append script like this
$('head').append($('<script>').attr('type', 'text/javascript').attr('src', '//code.jquery.com/jquery-1.9.1.js'));
To set date condition please refer this Link
One way to do this is to add the following code at the top of your index.html
/ index.php
file:
if(date("m.d") >= "12.15" && date("m.d") <= "01.15"){ // check the date
echo '<script type="text/javascript" src="snowstorm.js" ></script>'; // add the script if within the date range
}
Readup: date
| PHP