Skip to content

Instantly share code, notes, and snippets.

@wpeasy
Created June 21, 2025 04:02
Show Gist options
  • Select an option

  • Save wpeasy/91ba575832ac0fd79117f432366de75b to your computer and use it in GitHub Desktop.

Select an option

Save wpeasy/91ba575832ac0fd79117f432366de75b to your computer and use it in GitHub Desktop.
Bricks Date Comparison
<?php
add_filter("bricks/code/echo_function_names", function () {
return [
"wpe_date_is_eq_now",
"wpe_date_is_gt_now",
"wpe_date_is_gte_now",
"wpe_date_is_lt_now",
"wpe_date_is_lte_now",
"wpe_output_date_stats",
];
});
/**
* Check if given datetime is equal to current WordPress time
*/
function wpe_date_is_eq_now($date_string)
{
$target = strtotime($date_string);
$now = strtotime(date("Y-m-d 00:00:00", current_time("timestamp")));
return $target == $now;
}
/**
* Check if given datetime is greater than current WordPress time
*/
function wpe_date_is_gt_now($date_string)
{
$target = strtotime($date_string);
$now = strtotime(date("Y-m-d 00:00:00", current_time("timestamp")));
return $target > $now;
}
/**
* Check if given datetime is greater than or equal to current WordPress time
*/
function wpe_date_is_gte_now($date_string)
{
$target = strtotime($date_string);
$now = strtotime(date("Y-m-d 00:00:00", current_time("timestamp")));
return $target >= $now;
}
/**
* Check if given datetime is less than current WordPress time
*/
function wpe_date_is_lt_now($date_string)
{
$target = strtotime($date_string);
$now = strtotime(date("Y-m-d 00:00:00", current_time("timestamp")));
return $target < $now;
}
/**
* Check if given datetime is less than or equal to current WordPress time
*/
function wpe_date_is_lte_now($date_string)
{
$target = strtotime($date_string);
$now = strtotime(date("Y-m-d 00:00:00", current_time("timestamp")));
return $target <= $now;
}
/**
* Utility to check stats
*/
function wpe_output_date_stats($date_string)
{
$target = strtotime($date_string);
$now = strtotime(date("Y-m-d 00:00:00", current_time("timestamp")));
$out = '<table style="width: auto;">';
$out .=
'<tr><th style="text-align: right; padding: 5px;">Target</th><td style="padding: 5px;">' .
wpe_format_timestamp($target) .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">Now</th><td style="padding: 5px;">' .
wpe_format_timestamp($now) .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">TS Target</th><td style="padding: 5px;">' .
$target .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">TS Now</th><td style="padding: 5px;">' .
$now .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">wpe_date_is_eq_now</th><td style="padding: 5px;">' .
wpe_bool_to_str(wpe_date_is_eq_now($date_string)) .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">wpe_date_is_gt_now</th><td style="padding: 5px;">' .
wpe_bool_to_str(wpe_date_is_gt_now($date_string)) .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">wpe_date_is_gte_now</th><td style="padding: 5px;">' .
wpe_bool_to_str(wpe_date_is_gte_now($date_string)) .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">wpe_date_is_lt_now</th><td style="padding: 5px;">' .
wpe_bool_to_str(wpe_date_is_lt_now($date_string)) .
"</td></tr>";
$out .=
'<tr><th style="text-align: right; padding: 5px;">wpe_date_is_lte_now</th><td style="padding: 5px;">' .
wpe_bool_to_str(wpe_date_is_lte_now($date_string)) .
"</td></tr>";
$out .= "</table>";
return $out;
}
/** UTILS **/
function wpe_bool_to_str($bool)
{
return $bool ? "true" : "false";
}
function wpe_format_timestamp($timestamp)
{
return date_i18n(
get_option("date_format") . " " . get_option("time_format"),
$timestamp
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment