Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-04-24 09:10:37

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

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

#2 2012-04-24 09:31:26

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#3 2012-04-24 09:50:17

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,948
Website GitHub

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

#4 2012-04-24 09:57:55

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: xx days to go

Brilliant, that worked perfectly. Thanks Ruud!

Offline

#5 2012-04-24 12:12:10

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

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

#6 2012-04-24 12:26:54

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#7 2012-04-24 13:23:08

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: xx days to go

Cheers! All works.

Offline

Board footer

Powered by FluxBB