Contact Form 7 – Select drop down dates format

Sep 12, 2015 code

Updated: 06/11/2023

Contact Form 7 is great, but the format is slightly different than traditional forms. They wrap everything in quotes and it runs horizontal. So when it comes to dates, it can be quite tedious adding all that info in a format that isn’t easily accessible through a Google Search. In case you don’t want the datepicker and want to use a more traditional drop down for Month, Day, Year.

Here is a quick code to pop into Contact form 7. There are tons of code snippets for the traditional select drop down in HTML for dates but I didn’t find any out there when I was searching for Contact form 7’s format. Hopefully you will find this useful!

[select* menu-month "January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"][select* menu-day "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31"][select* menu-year "1950" "1951" "1952" "1953" "1954" "1955" "1956" "1957" "1958" "1959" "1960" "1961" "1962" "1963" "1964" "1965" "1966" "1967" "1968" "1969" "1970" "1971" "1972" "1973" "1974" "1975" "1976" "1977" "1978" "1979" "1980" "1981" "1982" "1983" "1984" "1985" "1986" "1987" "1988" "1989" "1990" "1991" "1992" "1993" "1994" "1995" "1996" "1997" "1998" "1999" "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009" "2010" "2011" "2012" "2013" "2014" "2015" "2016" "2017" "2018" "2019" "2020" "2021" "2022" "2023"]

If you need to customize the years, below are a few options with code. php, bash, python

Since WordPress is PHP I will show the PHP script first.

<?php

$months = array();
for ($i = 1; $i <= 12; $i++) {
    $months[] = '"' . date('F', mktime(0, 0, 0, $i, 1)) . '"';
}

$days = array();
for ($i = 1; $i <= 31; $i++) {
    $days[] = '"' . $i . '"';
}

$currentYear = date('Y');
$years = array();
for ($i = 1950; $i <= $currentYear; $i++) {
    $years[] = '"' . $i . '"';
}

$output = '[select* menu-month ' . implode(' ', $months) . '][select* menu-day ' . implode(' ', $days) . '][select* menu-year ' . implode(' ', $years) . ']';
echo $output;

In this bash script just adjust the start year to match your preferences. If you want the end year to not be the current year, you can adjust that as well.

#!/bin/bash

menu_month=""
menu_day=""
menu_year=""

for i in {1..12}; do
  if [[ $(uname) == "Darwin" ]]; then
    menu_month+="\"$(date -jf "%m" "$i" "+%B")\" "
  else
    menu_month+="\"$(date -d "01-$i-2023" "+%B")\" "
  fi
done

for i in {1..31}; do
  menu_day+="\"$i\" "
done

current_year=$(date +%Y)
for (( i = 1950; i <= current_year; i++ )); do
  menu_year+="\"$i\" "
done

echo "[select* menu-month $menu_month][select* menu-day $menu_day][select* menu-year $menu_year]"

If you prefer Python, here is a script below.

#!/usr/bin/env python3

import datetime

menu_month = [f'"{datetime.date(2023, i, 1).strftime("%B")}"' for i in range(1, 13)]
menu_day = [f'"{i}"' for i in range(1, 32)]
current_year = datetime.datetime.now().year
menu_year = [f'"{i}"' for i in range(1950, current_year + 1)]

output = f"[select* menu-month {' '.join(menu_month)}][select* menu-day {' '.join(menu_day)}][select* menu-year {' '.join(menu_year)}]"
print(output)