1. <?php
  2. /**
  3. * Encodes time values for short-lived tasks with verbose logging.
  4. *
  5. * @param int|float $timestamp The timestamp to encode (seconds since epoch).
  6. * @param string $format The desired output format (e.g., 'Y-m-d H:i:s', 'iso8601').
  7. * @return string The encoded time string.
  8. */
  9. function encodeTime(int|float $timestamp, string $format = 'Y-m-d H:i:s'): string
  10. {
  11. // Log the input timestamp and format.
  12. $logMessage = "Encoding time: Timestamp = " . $timestamp . ", Format = " . $format;
  13. error_log($logMessage);
  14. // Use date() to format the timestamp.
  15. $encodedTime = date($format, $timestamp);
  16. // Log the encoded time.
  17. $logMessage = "Encoded time: " . $encodedTime;
  18. error_log($logMessage);
  19. return $encodedTime;
  20. }
  21. // Example usage:
  22. $now = time();
  23. $encodedNow = encodeTime($now);
  24. echo "Encoded now: " . $encodedNow . "\n";
  25. $isoTime = encodeTime($now, 'Y-m-d\TH:i:sZ');
  26. echo "Encoded ISO: " . $isoTime . "\n";
  27. //Example with a specific timestamp
  28. $pastTime = strtotime("-1 hour");
  29. $encodedPast = encodeTime($pastTime);
  30. echo "Encoded past time: " . $encodedPast . "\n";
  31. ?>

Add your comment