Sort by Time Field

Sort times chronologically when populating a Time field from Gravity Forms entries into a choice based field.

Usage

  1. Install this code as a plugin or as a snippet.
  2. Change your form and field ID below

Code

Filename: gppa-sort-by-time-field.php

<?php
/**
 * Gravity Perks // Populate Anything // Sort by Time Field
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * Sort times chronologically when populating a Time field from Gravity Forms entries into a choice based field.
 *
 * Usage:
 *
 * 1. Install this code as a plugin or as a snippet.
 * 2. Change your form and field ID below
 */
// Change `123` to your form ID and `4` to your populated field ID
add_filter( 'gppa_input_choices_123_4', function( $choices, $field, $objects, $field_values ){

	usort( $choices, function( $a, $b ) {
		$timeA = strtotime( $a['text'] );
		$timeB = strtotime( $b['text'] );
		return $timeA - $timeB;
	});

	return $choices;

}, 10, 4 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.