User Tools

Site Tools


coding:what_year

What Year?

Someone in passing mentioned they had a project that could tell them the year of a vintage gig poster - I'd already built something into a bot to answer something similar when I was working on an events project, if I saw a poster in a bar and wondered if it was upcoming or not, I could ask “Adelaide is Fri Dec 1st this year?” and get a yes or no.

Due to how the string to time function in PHP works, it's not too difficult. Here is an expansion of the code to work for either a “day month date” or even just a “date month”.

For example if the poster has “Playing Tuesday 31st December” you can tell which year, and if it just has “Playing this Tuesday 31st” you can make a guess:

what_year.php
<?php
// Usage: ./what_year.php Tue 31st
//    or  ./what_year.php Tue 31st Dec 
$goal = implode(" ", array_slice($argv, 1, sizeof($argv)));
$pattern = "D jS";
if(sizeof($argv)>3)
	$pattern = "D jS M";
echo "Goal is $goal, pattern is $pattern\n";
for($i=1960; $i<=2025; $i++) {
	$start_date = date("Y-m-d", strtotime("$i-01-01"));
	for($day=0;$day<=365;$day++) {
		$formatted_date = date($pattern, strtotime($start_date." +$day days"));
		if($formatted_date == $goal) {
			echo $goal ." occurs in ".date("M Y", strtotime($start_date." +$day days"))."\n";
		}
	}
}
?>

The output for ./what_year.php Tue 31st Dec is:

Tue 31st Dec occurs in Dec 1963
Tue 31st Dec occurs in Dec 1968
Tue 31st Dec occurs in Dec 1974
Tue 31st Dec occurs in Dec 1985
Tue 31st Dec occurs in Dec 1991
Tue 31st Dec occurs in Dec 1996
Tue 31st Dec occurs in Dec 2002
Tue 31st Dec occurs in Dec 2013
Tue 31st Dec occurs in Dec 2019
Tue 31st Dec occurs in Dec 2024

The output for ./what_year.php Tue 31st is:

# *snipped*
Tue 31st occurs in Mar 2015
Tue 31st occurs in May 2016
Tue 31st occurs in Jan 2017
Tue 31st occurs in Oct 2017
Tue 31st occurs in Jul 2018
Tue 31st occurs in Dec 2019
Tue 31st occurs in Mar 2020
Tue 31st occurs in Aug 2021
Tue 31st occurs in May 2022
Tue 31st occurs in Jan 2023
Tue 31st occurs in Oct 2023
Tue 31st occurs in Dec 2024

Back to PHP teaching programs

coding/what_year.txt · Last modified: 2025/03/26 22:39 by admin