MrQwest

Automating Recurring Event Dates with PHP

As you may or may not know, I’m developing my skill set beyond the front-end. I’ve always wanted to learn back-end technologies; languages capable of performing the function behind our sites. I wanted to be able to design a side project, complete the front-end build and the back end function.

I’ve been able to do this via the awesome Treehouse. Long story short, my car experienced a slew of problems including needing a new turbo & gearbox. Whilst the car was in the garage, I started taking the train to London for work. This gave me 20 minutes or so each way as free time. I chose to watch a bunch of videos on various languages and now I’m putting what I learnt into practice!

So back to the point of this post. I also run a local social for web & creative folk called Croydon Creativ.es – It’s a chance to get away from the screen with like-minded individuals, drink beer (or coffee) whilst sharing stories, tricks and banter — It’s good fun, you should come!

Every month, I’d fire up terminal, pull the site down from Github, open up the index, change the date for the next event, save, commit, push and ftp it back to the server. It’s an easy 10 minute thing but we all know 10 minute jobs aren’t really 10 minute jobs.

So to put my newly gained PHP skills to the test, I set about creating a solution which should only need looking at once or twice a year.

First step was to create a few variables.

// the current date & time
$now = date(DATE_ATOM);
// Here's the array of dates for the events
$dates = array(
  date(DATE_ATOM, mktime(19, 30, 0, 09, 24, 2014)), 
  date(DATE_ATOM, mktime(19, 30, 0, 08, 31, 2014)),
  date(DATE_ATOM, mktime(19, 30, 0, 10, 29, 2014)), 
  date(DATE_ATOM, mktime(19, 30, 0, 11, 26, 2014)), 
  date(DATE_ATOM, mktime(19, 30, 0, 12, 17, 2014)), 
  date(DATE_ATOM, mktime(19, 30, 0, 01, 28, 2015)), 
  date(DATE_ATOM, mktime(19, 30, 0, 02, 25, 2015)), 
  date(DATE_ATOM, mktime(19, 30, 0, 03, 25, 2015)), 
  date(DATE_ATOM, mktime(19, 30, 0, 04, 29, 2015)), 
  date(DATE_ATOM, mktime(19, 30, 0, 05, 27, 2015)), 
  date(DATE_ATOM, mktime(19, 30, 0, 06, 24, 2015))
);
// initialise empty array for future events
$futuredates = array();

The comments should be self explanatory but $now sets the current date. $dates is an array which holds the next 9 months worth of events. $futuredates is an empty array that I’ll use later on.

The next section is a foreach loop and is effectively the core of this bit of code.

// the foreach
  foreach($dates as $date){
    // if the date from the $dates array is older than today...
  	if ($date < $now ){
  		// donothing
  	} else {
  	  // if it's a future event, push the date into $futuredates
  		array_push($futuredates, $date);
  	};
  };

The foreach takes every item within the $dates array and runs an if statement against each one. So if the date is less than $now (ie: older than today), it’s ignored. If it’s greater than $now, the date is pushed into our previously set up empty array, $futuredates.

So now, we can run <?php echo $futuredates[0]; ?> and it should output the next event date.

We could leave it like that but the PHP date format here outputs as 2014-10-29T19:30:00 which isn’t ideal. I want to format it to something like “29th Oct”.

// convert the first item from the futuredates array as a string, ie: the next event.
  $nextDate = strtotime($futuredates[0]); 

So here we’re converting the next event date to a Unix timestamp (a string of numbers which relates to the amount of seconds since 1st Jan, 1970). From this, we can convert the date to two separate formats required.

The Next Event section on the CroydonCreativ.es site is just a date displayed as “29th Oct @ 7pm(ish)”. The time stays the same so I’m looking to just dynamically insert the “29th Oct” bit.

// date as human readable, 29th Oct
$showNextDate = date("jS M",$nextDate);

Here, we’re taking the date() function, parsing the format we want (in this instance, it’s jS M – the documentation on date() is excellent and shows all the parameters you can use!).

The last item here is that my displayed date is wrapped in a <time> element. The format required here is different.

// time as a string for <time> element.
$timeNextDate = date("c",$nextDate);

Fortunately for me, there’s a time format on date() which outputs the string I need, the ISO8601 date in full. So this literally outputs 2014-10-29T19:30:00+00:00 perfect!

All that’s left is to pull these into my HTML like so.

<section id="next">
  <h1>Next: <time datetime="<?php echo $timeNextDate; ?>"><a href="<?php echo $attendlink; ?>"><?php echo $showNextDate; ?> @ 7pm (ish)</a></time></h1>
</section>

Excellent!

It works but needs a bit of testing. It’s also probably full of bad practices. I’m still learning here! If you can see improvements, please jump onto github and do a pull request!


More in the Archive