1. <?php
  2. /**
  3. * Restores date values from a configuration file for dry-run scenarios.
  4. *
  5. * @param string $config_file Path to the configuration file.
  6. * @return array|false An array of date/time overrides, or false on error.
  7. */
  8. function restoreDateValues(string $config_file): array|false
  9. {
  10. if (!file_exists($config_file)) {
  11. error_log("Error: Configuration file not found at $config_file");
  12. return false;
  13. }
  14. $config = require($config_file);
  15. if (!is_array($config) || !isset($config['date_overrides'])) {
  16. error_log("Error: Invalid configuration format. Missing 'date_overrides' array.");
  17. return false;
  18. }
  19. $date_overrides = $config['date_overrides'];
  20. $overrides = [];
  21. foreach ($date_overrides as $date_string => $timestamp) {
  22. // Attempt to convert the date string to a Unix timestamp
  23. $timestamp_value = strtotime($date_string);
  24. if ($timestamp_value === false) {
  25. error_log("Warning: Invalid date format '$date_string' in configuration. Skipping.");
  26. continue; // Skip to the next date if parsing fails
  27. }
  28. $overrides[$date_string] = $timestamp_value;
  29. }
  30. return $overrides;
  31. }
  32. /**
  33. * Applies the date overrides to the current time.
  34. *
  35. * @param array $overrides An array of date/time overrides.
  36. */
  37. function applyDateOverrides(array $overrides): void
  38. {
  39. foreach ($overrides as $date_string => $timestamp) {
  40. // Set the current time to the specified timestamp
  41. if (date_create_from_timestamp($timestamp)) {
  42. //Use date_default_setzone() to avoid timezone issues
  43. date_default_setzone();
  44. //Set the current time
  45. date_timezone_set('UTC'); //or appropriate timezone
  46. //For demonstration purposes, we just set the current time here.
  47. //In a real application, you would adjust the relevant date/time values.
  48. //Example: $my_date_field = date('Y-m-d H:i:s', $timestamp);
  49. } else {
  50. error_log("Warning: Invalid timestamp '$timestamp' for date '$date_string'. Skipping.");
  51. }
  52. }
  53. }
  54. // Example Usage (replace with your actual config file)
  55. $config_file = 'config.php'; // Example config file
  56. $date_overrides = restoreDateValues($config_file);
  57. if ($date_overrides !== false) {
  58. applyDateOverrides($date_overrides);
  59. echo "Date overrides applied successfully.\n";
  60. } else {
  61. echo "Failed to restore date values.\n";
  62. }
  63. ?>

Add your comment