Calculating the day-of-the-year

This short example makes it possible to calculate and display the day of the year for a date entered on a form.

This form will be named "calculate_doy", and can then be called from within a SPIP template file with #FORMULAIRE_CALCULATE_DOY or within the text of an article by using <formulaire|calculate_doy>.


The two files necessary will be created as follows:

  • formulaires/calculate_doy.html for the HTML section
  • formulaires/calculate_doy.php for the PHP analysis and processing the CVT functions.

The HTML template file

The formulaires/calculate_doy.html file contains the following code, respecting the recommended HTML structure and CSS classes:

<div class="formulaire_spip formulaire_#FORM">

[<p class="reponse_formulaire reponse_formulaire_ok">(#ENV*{message_ok})</p>]
[<p class="reponse_formulaire reponse_formulaire_erreur">(#ENV*{message_erreur})</p>]

<form name="formulaire_#FORM" action="#ENV{action}" method="post"><div>

	<li class="editer_date_jour obligatoire[ (#ENV**{erreurs}|table_valeur{message}|oui)erreur]">	
		<label for="champ_date_jour">Date (dd/mm/yyyy) :</label>
		[<span class='erreur_message'>(#ENV**{erreurs}|table_valeur{message})</span>]
		<input type="text" id="champ_date_jour" name="date_jour" value="[(#ENV{date_jour})]" />
	<p class="boutons">
		<input type="submit" name="ok" value="Calculate" />

Note that the "Saisies" plugin can be used to write the form’s fields using a #SAISIE tag, and specifying the type and name of the variable used, followed by whichever optional parameters are useful. Doing so would produce (the code section between <ul> and </ul>):

[(#SAISIE{input, date_jour, obligatoire=oui, label="Date (dd/mm/yyyy) :"})]

Loading, verifying and processing

The formulaires/calculate_doy.php file contains the three following functions:

The "loading" file lists the variables which will be passed into the template environment and initialises their default values. There is no default date here, but it would be possible to specify one if you wanted.

function formulaires_calculate_doy_charger_dist (){
	$valeurs = array(
		'date_jour' => ''
	return $valeurs;

The "verify" function checks to make sure the compulsory fields are entered and that the date format appears to be correct:

function formulaires_calculate_doy_verifier_dist (){
	$erreurs = array();
	// compulsory fields
	foreach(array ('date_jour') as $obligatoire) {
		if (!_request($obligatoire)) $erreurs[$obligatoire] = 'This field is compulsory';
	// correct date format
	if (!isset($erreurs['date_jour'])) {
		list($jour, $mois,U $annee) = explode('/', _request('date_jour'));
		if (!intval($jour) or !intval($mois) or !intval($annee)) {
			$erreurs['date_jour'] = "Unknown date format.";
	if (count($erreurs)) {
		$erreurs['message_erreur'] = 'Your data contains errors!';
	return $erreurs;

If the verifications are correct (no errors found), then the "process" function is executed. The form is declared as re-editable, which means that a new date value can be entered again immediately after the validation.

function formulaires_calculate_doy_traiter_dist (){
	$date_jour	 = _request('date_jour');
	$retour = array('editable' => true);
	if ($doy = calculate_doy($date_jour)) { 
		$retour['message_ok'] = "The day of the year for $date_jour is $doy";
	} else {
		$retour['message_erreur'] = "DOY calculation error!";
	return $retour;

Of course, this still omits the function used to calculate the day-of-the-year, but a few simple lines of PHP will fix that. This function can be implemented in the same file as the three previous functions:

function calculate_doy($date_jour) {
	list($jour, $mois, $annee) = explode('/', $date_jour);
	if ($time = mktime( 0, 0, 0, $mois, $jour, $annee)) {
		return date('z', $time);
	return false;

Author Mark Baber Published : Updated : 12/03/23

Translations : English, français