<?php
/**
* Encodes time values for short-lived tasks with verbose logging.
*
* @param int|float $timestamp The timestamp to encode (seconds since epoch).
* @param string $format The desired output format (e.g., 'Y-m-d H:i:s', 'iso8601').
* @return string The encoded time string.
*/
function encodeTime(int|float $timestamp, string $format = 'Y-m-d H:i:s'): string
{
// Log the input timestamp and format.
$logMessage = "Encoding time: Timestamp = " . $timestamp . ", Format = " . $format;
error_log($logMessage);
// Use date() to format the timestamp.
$encodedTime = date($format, $timestamp);
// Log the encoded time.
$logMessage = "Encoded time: " . $encodedTime;
error_log($logMessage);
return $encodedTime;
}
// Example usage:
$now = time();
$encodedNow = encodeTime($now);
echo "Encoded now: " . $encodedNow . "\n";
$isoTime = encodeTime($now, 'Y-m-d\TH:i:sZ');
echo "Encoded ISO: " . $isoTime . "\n";
//Example with a specific timestamp
$pastTime = strtotime("-1 hour");
$encodedPast = encodeTime($pastTime);
echo "Encoded past time: " . $encodedPast . "\n";
?>
Add your comment