Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
xx days to go
A client wants me to make a ‘xx days to go’ statement on their site, so a countdown of days to go until a specific date. The date being the 21st May 2012.
I’m pretty sure I can do this with a combination <txp:variable />
tags and smd_if
plugin but I’m having brain freeze today. Has anyone got an example of how I could achieve this? I don’t want to add any other third-party plugins if I don’t have to.
Offline
Re: xx days to go
Something like this perhaps (not tested):
<txp:php>
$doomsday = mktime(0,0,0,5,21,2012);
$now = time();
$daystogo = max(0, ceil( ($doomsday - $now) / (24*60*60) ) );
echo $daystogo;
</txp:php>
ceil
ensures that one minute before midnight it still says 1 day to go (instead of rounding down to 0)
max
ensures that in the days beyond doomsday, it still says 0 days to go instead of going negative.
Btw. this only outputs the number of days. If you want to display something only until the day is reached, replace the entire echo line with:
if ($daystogo) echo "<p>$daystogo days left till doomsday</p>";
And if you want an alternate message shown when doomsday is reached, add the following line below that:
else echo "<p>If you see this, we were wrong about the doomsday date. Sorry.</p>";
Offline
Re: xx days to go
philwareham wrote:
A client wants me to make a ‘xx days to go’ statement on their site
Or smd_countdown if you decide a third party plugin makes your life easier / you intend to reuse the concept elsewhere on the site in future.
Last edited by Bloke (2012-04-24 09:51:45)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: xx days to go
Brilliant, that worked perfectly. Thanks Ruud!
Offline
Re: xx days to go
Could I ask one more question… in a similar fashion to above, how could I get a class="upcoming-event"
on an element when a date criteria is met. Say for example up to 8th May 2012 that class appears on a div element, afterwards it doesn’t?
So, up to and including 8th May 2012 the div looks like…
<div class="upcoming-event">...</div>
…and then afterwards, just as…
<div>...</div>
(I wish I knew PHP)
Last edited by philwareham (2012-04-24 12:12:32)
Offline
Re: xx days to go
<div<txp:php>
$doomsday = mktime(0,0,0,5,8,2012);
$now = time();
$daystogo = max(0, ceil( ($doomsday - $now) / (24*60*60) ) );
if ($daystogo) echo ' class="upcoming-event"';
</txp:php>>...</div>
Or, using smd_countdown (didn’t know it existed till now):
<div<txp:smd_countdown to="8 May 2012"><txp:else /> class="upcoming-event"</txp:smd_countdown>>...</div>
Offline
Re: xx days to go
Cheers! All works.
Offline
Pages: 1